I searched for getting the EXIF data from picture files and write them back for Swift. But I only could find predefied libs for different languages.
I also found references to "CFDictionaryGetValue", but which keys do I need to get the data? And how can I write it back?
I'm using this to get EXIF infos from an image file:
import ImageIO let fileURL = theURLToTheImageFile if let imageSource = CGImageSourceCreateWithURL(fileURL as CFURL, nil) { let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) if let dict = imageProperties as? [String: Any] { print(dict) } }
It gives you a dictionary containing various informations like the color profile - the EXIF info specifically is in dict["{Exif}"]
.
Swift 4
extension UIImage { func getExifData() -> CFDictionary? { var exifData: CFDictionary? = nil if let data = self.jpegData(compressionQuality: 1.0) { data.withUnsafeBytes {(bytes: UnsafePointer<UInt8>)->Void in if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.count) { let source = CGImageSourceCreateWithData(cfData, nil) exifData = CGImageSourceCopyPropertiesAtIndex(source!, 0, nil) } } } return exifData } }
Swift 5
extension UIImage { func getExifData() -> CFDictionary? { var exifData: CFDictionary? = nil if let data = self.jpegData(compressionQuality: 1.0) { data.withUnsafeBytes { let bytes = $0.baseAddress?.assumingMemoryBound(to: UInt8.self) if let cfData = CFDataCreate(kCFAllocatorDefault, bytes, data.count), let source = CGImageSourceCreateWithData(cfData, nil) { exifData = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) } } } return exifData } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With