Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mcxiaoke/da1eb2cc4a78ce9711b03a6b74d857ef to your computer and use it in GitHub Desktop.
Save mcxiaoke/da1eb2cc4a78ce9711b03a6b74d857ef to your computer and use it in GitHub Desktop.
Modifying EXIF Data with Swift 3
//: Playground - noun: a place where people can play
import UIKit
import ImageIO
import MobileCoreServices
let TEST_IMAGE: String = "replace.jpg"
let beach: UIImage = UIImage(named: TEST_IMAGE)!
let imageData: Data = UIImageJPEGRepresentation(beach, 1)!
let cgImgSource: CGImageSource = CGImageSourceCreateWithData(imageData as CFData, nil)!
let uti: CFString = CGImageSourceGetType(cgImgSource)!
let dataWithEXIF: NSMutableData = NSMutableData(data: imageData)
let destination: CGImageDestination = CGImageDestinationCreateWithData((dataWithEXIF as CFMutableData), uti, 1, nil)!
let imageProperties = CGImageSourceCopyPropertiesAtIndex(cgImgSource, 0, nil)! as NSDictionary
let mutable: NSMutableDictionary = imageProperties.mutableCopy() as! NSMutableDictionary
var EXIFDictionary: NSMutableDictionary = (mutable[kCGImagePropertyExifDictionary as String] as? NSMutableDictionary)!
print("before modification \(EXIFDictionary)")
EXIFDictionary[kCGImagePropertyExifUserComment as String] = "type:video"
mutable[kCGImagePropertyExifDictionary as String] = EXIFDictionary
CGImageDestinationAddImageFromSource(destination, cgImgSource, 0, (mutable as CFDictionary))
CGImageDestinationFinalize(destination)
let testImage: CIImage = CIImage(data: dataWithEXIF as Data, options: nil)!
let newproperties: NSDictionary = testImage.properties as NSDictionary
print("after modification \(newproperties)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment