不使用UIImagePickerController

时间:2017-03-17 14:45:01

标签: ios swift uiimage

如何使用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)
}

1 个答案:

答案 0 :(得分:4)

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

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)")
        }
    }
}