24

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?

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
Peter Silie
  • 825
  • 1
  • 11
  • 16

3 Answers3

37

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

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
10

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
    }
}
Abhijeet Rai
  • 119
  • 1
  • 5
  • 2
    For Swift 5+ you want to change the `data.withUnsafeBytes { (bytes: UnsafePointer)->Void in ...` to `data.withUnsafeBytes { let bytes = $0.baseAddress?.assumingMemoryBound(to: UInt8.self) ... }` – Teo Sartori Dec 12 '19 at 10:52
  • This method retrieves less exif data than the accepted one – Lubbo Sep 14 '22 at 10:35
1

You can use AVAssetExportSession to write metadata.

let asset = AVAsset(url: existingUrl)
let exportSession = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality)
exportSession?.outputURL = newURL
exportSession?.metadata = [
  // whatever [AVMetadataItem] you want to write
]
exportSession?.exportAsynchronously {
  // respond to file writing completion
}
abcross92
  • 33
  • 5