I am using PhotoKit to edit photos and I need to preserve the metadata from the original photo. To do so I save the metadata then provide it to the options
paramete
It seems that filling the adjustementData
property of the PHContentEditingOutput
object is mandatory in order to edit a photo.
Certain keys in the metadata seem to cause failure. This works:
NSArray *validMetadataKeys = @[
(NSString *)kCGImagePropertyTIFFDictionary,
(NSString *)kCGImagePropertyGIFDictionary,
(NSString *)kCGImagePropertyJFIFDictionary,
(NSString *)kCGImagePropertyExifDictionary,
(NSString *)kCGImagePropertyPNGDictionary,
(NSString *)kCGImagePropertyIPTCDictionary,
(NSString *)kCGImagePropertyGPSDictionary,
(NSString *)kCGImagePropertyRawDictionary,
(NSString *)kCGImagePropertyCIFFDictionary,
(NSString *)kCGImageProperty8BIMDictionary,
(NSString *)kCGImagePropertyDNGDictionary,
(NSString *)kCGImagePropertyExifAuxDictionary,
];
NSMutableDictionary *validMetadata = [NSMutableDictionary dictionary];
[metadata enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([validMetadataKeys containsObject:key]) {
validMetadata[key] = obj;
}
}];
// your CGImage stuff
CGImageDestinationAddImageFromSource(destination, imgSource, 0, (__bridge CFDictionaryRef)validMetadata);
We are supposed to write the data for the new image to the URL we obtain via
let theExportURL = output.renderedContentURL
In my case i was doing this...
let outputData = UIImagePNGRepresentation(bakedImage)
And at runtime i was getting the error
Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn’t be completed. (Cocoa error -1.)
But when I started exporting the data like this...
let outputData = UIImageJPEGRepresentation(bakedImage, 1)
It worked. This is documented here ... https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHContentEditingOutput_Class/index.html
and this same question has been answered here... https://stackoverflow.com/a/28362235
I have exactly the same problem, and I have filed a radar rdar://21057247. I try to log a case using TSI but I have been told to file a radar instead.
// if the asset does not allow the type of change requested, these methods will raise an exception, call canPerformEditOperation: on the asset to determine if the type of edit operation is allowed.
I think it's because Apple don't want you to change the metaData.You can try this method below,maybe create a new one is allowed.