Applying metadata to image causes performChanges request to fail

前端 未结 5 621
感动是毒
感动是毒 2021-02-06 15:29

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

相关标签:
5条回答
  • 2021-02-06 15:53

    It seems that filling the adjustementData property of the PHContentEditingOutput object is mandatory in order to edit a photo.

    0 讨论(0)
  • 2021-02-06 15:57

    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);
    
    0 讨论(0)
  • 2021-02-06 15:59

    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

    0 讨论(0)
  • 2021-02-06 16:04

    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.

    0 讨论(0)
  • 2021-02-06 16:08

    // 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.

    • (instancetype)changeRequestForAsset:(PHAsset *)asset;

    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.

    • (instancetype)creationRequestForAssetFromImage:(UIImage *)image;
    • (instancetype)creationRequestForAssetFromImageAtFileURL:(NSURL *)fileURL;
    • (instancetype)creationRequestForAssetFromVideoAtFileURL:(NSURL *)fileURL;
    0 讨论(0)
提交回复
热议问题