Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXIF data read and write

Tags:

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?

like image 324
Peter Silie Avatar asked Oct 21 '16 11:10

Peter Silie


Video Answer


2 Answers

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}"].

like image 119
Eric Aya Avatar answered Sep 20 '22 06:09

Eric Aya


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     } } 
like image 35
Abhijeet Rai Avatar answered Sep 17 '22 06:09

Abhijeet Rai