Search code examples
iosswiftuiimage

Get UIImage metadata without using UIImagePickerController


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

Solution

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