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
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);