EXIF data read and write

11,333

Solution 1

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

Solution 2

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

Solution 3

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
}
Share:
11,333

Related videos on Youtube

Peter Silie
Author by

Peter Silie

Updated on October 29, 2022

Comments

  • Peter Silie
    Peter Silie 10 days

    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
      Léo Natan about 6 years
      There is already a swift tag. What is the point of including that in the question title?
    • Peter Silie
      Peter Silie about 6 years
      Ok, I wanted to indicate that language matters. I will not use it in future.
    • Peter Silie
      Peter Silie about 6 years
      Sure, I take what I can get. :-) I will see how far I get with it.
  • Genfood almost 5 years
    How could I write the EXIF data back to the image?
  • Amos
    Amos about 4 years
    this answer didn't mention "write", anyone knows it?
  • Eric Aya
    Eric Aya about 4 years
    @Amos See stackoverflow.com/q/37992611/2227743 and stackoverflow.com/q/43920643/2227743 it looks like you can use these Q&A to find out how to write the exif.
  • Areal-17 over 3 years
    You can also user kCGImagePropertyExifDictionary as key.
  • Teo Sartori
    Teo Sartori almost 3 years
    For Swift 5+ you want to change the data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>)->Void in ... to data.withUnsafeBytes { let bytes = $0.baseAddress?.assumingMemoryBound(to: UInt8.self) ... }