I am editing photos via PhotoKit but I discovered this does not preserve the original photo's metadata. This occurs even with the SamplePhotosApp provided by Apple when they apply Sepia or Chrome filters. My question is, how do you ensure all the original photo metadata is preserved?
I've discovered how you can obtain the original image's metadata, and I was able to save that metadata to the final CIImage
I create, but it still is stripped out when the edit is committed. There must be an issue in the way I convert the CIImage
to a CGImage
to a UIImage
to NSData
, or how I'm writing it to disk.
asset.requestContentEditingInputWithOptions(options) { (input: PHContentEditingInput!, _) -> Void in
//Get full image
let url = input.fullSizeImageURL
let orientation = self.input.fullSizeImageOrientation
var inputImage = CIImage(contentsOfURL: url)
inputImage = inputImage.imageByApplyingOrientation(orientation)
//do some processing on original photo here and create a CGImage...
//save the original photo's metadata to a new CIImage:
let originalMetadata = inputImage.properties()
let newImage = CIImage(CGImage: editedCGImage, options: [kCIImageProperties: originalMetadata])
println(newImage.properties()) //correctly prints all metadata!
//commit changes to disk - somewhere after this line the metadata is lost
let eaglContext = EAGLContext(API: .OpenGLES2)
let ciContext = CIContext(EAGLContext: eaglContext)
let outputImageRef = ciContext.createCGImage(newImage, fromRect: newImage.extent())
let uiImage = UIImage(CGImage: outputImageRef, scale: 1.0, orientation: UIImageOrientation.Up)
let jpegNSData = UIImageJPEGRepresentation(uiImage, 0.75)
let contentEditingOutput = PHContentEditingOutput(contentEditingInput: input)
let success = jpegData.writeToURL(contentEditingOutput.renderedContentURL, options: NSDataWritingOptions.AtomicWrite, error: _)
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let request = PHAssetChangeRequest(forAsset: asset)
request.contentEditingOutput = contentEditingOutput
}, completionHandler: { (success: Bool, error: NSError!) -> Void in
if success == false { println('failed to commit image edit: \(error)') }
})
})
Original - note the GPS tab:
After editing the photo: