2

How to get the UIImage metadata WITHOUT using UIImagePickerController.

I have basically a variable UIImage and I want to get the metadata.

I would need something like "myImage.date" to access the date for example.

EDIT 1 : I try this but it crash (found nil on exif var)

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)
}
user3722523
  • 1,740
  • 2
  • 15
  • 27
  • I have my variable UIImage which contains an image taken from the phone and from there I need to get the metadata. Dude I search on the net but everyone is using the uiimagepickercontroller delegate. I think it's useless to make a video to prove you my research on google.com – user3722523 Mar 17 '17 at 14:57
  • Is this what you need? http://stackoverflow.com/q/40175160/2227743 – Eric Aya Mar 17 '17 at 14:59
  • thank you it looks like what I need, now need to try with an UIImage – user3722523 Mar 17 '17 at 15:12

1 Answers1

9

You might find this a useful starting point…

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)")
        }
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • 3
    Thx I got many properties but I don't have the date, I don't know if I can get the date of an UIImage. Maybe I must have a PHAsset instead of UIImage ? – user3722523 Mar 17 '17 at 20:02