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
}
Related videos on Youtube

Author by
Peter Silie
Updated on October 29, 2022Comments
-
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 about 6 yearsThere is already a
swift
tag. What is the point of including that in the question title? -
Peter Silie about 6 yearsOk, I wanted to indicate that language matters. I will not use it in future.
-
Peter Silie about 6 yearsSure, I take what I can get. :-) I will see how far I get with it.
-
-
Genfood almost 5 yearsHow could I write the EXIF data back to the image?
-
Amos about 4 yearsthis answer didn't mention "write", anyone knows it?
-
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 yearsYou can also user kCGImagePropertyExifDictionary as key.
-
Teo Sartori almost 3 yearsFor Swift 5+ you want to change the
data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>)->Void in ...
todata.withUnsafeBytes { let bytes = $0.baseAddress?.assumingMemoryBound(to: UInt8.self) ... }