不使用UIImagePickerController获取UIImage元数据

问题描述 投票:0回答:2

如何在不使用 UIImagePickerController 的情况下获取 UIImage 元数据。

我基本上有一个变量 UIImage,我想获取元数据。

例如,我需要类似“myImage.date”的内容来访问日期。

编辑1:我尝试了这个,但它崩溃了(在 exif var 上发现 nil)

func getData(image: UIImage) {
    guard let data = UIImageJPEGRepresentation(image, 1),
        let source = CGImageSourceCreateWithData(data as CFData, nil) else { return}


    let imageProperties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil)
    let a = Unmanaged.passUnretained(kCGImagePropertyExifDateTimeOriginal).toOpaque()
    let exif = CFDictionaryGetValue(imageProperties, a)
    print(exif)
}
ios swift uiimage
2个回答
9
投票

您可能会发现这是一个有用的起点......

func getMetaData(forImage image: UIImage) {
    guard let data = UIImageJPEGRepresentation(image, 1),
        let source = CGImageSourceCreateWithData(data as CFData, nil) else { return}

    if let type = CGImageSourceGetType(source) {
        print("type: \(type)")
    }

    if let properties = CGImageSourceCopyProperties(source, nil) {
        print("properties - \(properties)")
    }

    let count = CGImageSourceGetCount(source)
    print("count: \(count)")

    for index in 0..<count {
        if let metaData = CGImageSourceCopyMetadataAtIndex(source, index, nil) {
            print("all metaData[\(index)]: \(metaData)")

            let typeId = CGImageMetadataGetTypeID()
            print("metadata typeId[\(index)]: \(typeId)")


            if let tags = CGImageMetadataCopyTags(metaData) as? [CGImageMetadataTag] {

                print("number of tags - \(tags.count)")

                for tag in tags {

                    let tagType = CGImageMetadataTagGetTypeID()
                    if let name = CGImageMetadataTagCopyName(tag) {
                        print("name: \(name)")
                    }
                    if let value = CGImageMetadataTagCopyValue(tag) {
                        print("value: \(value)")
                    }
                    if let prefix = CGImageMetadataTagCopyPrefix(tag) {
                        print("prefix: \(prefix)")
                    }
                    if let namespace = CGImageMetadataTagCopyNamespace(tag) {
                        print("namespace: \(namespace)")
                    }
                    if let qualifiers = CGImageMetadataTagCopyQualifiers(tag) {
                        print("qualifiers: \(qualifiers)")
                    }
                    print("-------")
                }
            }
        }

        if let properties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) {
            print("properties[\(index)]: \(properties)")
        }
    }
}

0
投票

斯威夫特5

func getMetaData(forImage image: UIImage) {
    guard let data = image.jpegData(compressionQuality: 1),
        let source = CGImageSourceCreateWithData(data as CFData, nil) else {
        return
    }

    /// Returns the uniform type identifier of the source container.
    if let type = CGImageSourceGetType(source) {
        print("type: \(type)")
    }

    /// Returns the properties of the image source.
    if let properties = CGImageSourceCopyProperties(source, nil) {
        print("properties - \(properties)")
    }

    /// Returns the number of images (not including thumbnails) in the image source.
    let count = CGImageSourceGetCount(source)
    print("count: \(count)")

    for index in 0..<count {
        if let metaData = CGImageSourceCopyMetadataAtIndex(source, index, nil) {
            print("all metaData[\(index)]: \(metaData)")

            /// Returns the type identifier for metadata objects.
            let typeId = CGImageMetadataGetTypeID()
            print("metadata typeId[\(index)]: \(typeId)")

            /// Returns an array of root-level metadata tags from the specified metadata object.
            if let tags = CGImageMetadataCopyTags(metaData) as? [CGImageMetadataTag] {

                print("number of tags - \(tags.count)")

                for tag in tags {
                    /// Returns an immutable copy of the tag’s name.
                    if let name = CGImageMetadataTagCopyName(tag) {
                        print("name: \(name)")
                    }
                    /// Returns a shallow copy of the tag’s value, which is suitable only for reading.
                    if let value = CGImageMetadataTagCopyValue(tag) {
                        print("value: \(value)")
                    }
                    /// Returns an immutable copy of the tag’s prefix.
                    if let prefix = CGImageMetadataTagCopyPrefix(tag) {
                        print("prefix: \(prefix)")
                    }
                    /// Returns an immutable copy of the tag’s XMP namespace.
                    if let namespace = CGImageMetadataTagCopyNamespace(tag) {
                        print("namespace: \(namespace)")
                    }
                    /// Returns a shallow copy of the metadata tags that act as qualifiers for the current tag.
                    if let qualifiers = CGImageMetadataTagCopyQualifiers(tag) {
                        print("qualifiers: \(qualifiers)")
                    }
                    print("-------")
                }
            }
        }

        /// Returns the properties of the image at a specified location in an image source.
        if let properties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) {
            print("properties[\(index)]: \(properties)")
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.