将元数据应用于图像导致performChanges请求失败

我正在使用PhotoKit编辑照片,我需要保留原始照片的元数据。 为此,我保存元数据,然后将其提供给CGImageDestinationAddImageoptions参数。 我能够最终确定并成功将其写入磁盘,但是当我调用performChanges来提交资源编辑时,它会失败。 如果我提供nil options ,它将成功。 这里怎么了?

 asset.requestContentEditingInputWithOptions(options) { (input: PHContentEditingInput!, _) -> Void in //get full image let url = input.fullSizeImageURL let inputImage = CIImage(contentsOfURL: url) //get orginal photo's metadata let originalImageData = NSData(contentsOfURL: url)! let imageSource = CGImageSourceCreateWithData(originalImageData, nil) let metadata: CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) println(metadata) //prints all the metadata, yay! //do some processing on original photo here and create an output CIImage... //save to disk let dataRef = CFDataCreateMutable(nil, 0) let destination = CGImageDestinationCreateWithData(dataRef, CGImageSourceGetType(imageSource), 1, nil) let eaglContext = EAGLContext(API: .OpenGLES2) let ciContext = CIContext(EAGLContext: eaglContext) let cgImage = ContextStruct.ciContext!.createCGImage(outputPhoto, fromRect: outputPhoto.extent()) CGImageDestinationAddImage(destination, cgImage, metadata) //metadata is problematic - replacing with nil causes it to work if CGImageDestinationFinalize(destination) { let contentEditingOutput = PHContentEditingOutput(contentEditingInput: input) contentEditingOutput.adjustmentData = "something" let imageData: NSData = dataRef let success = imageData.writeToURL(contentEditingOutput.renderedContentURL, options: .AtomicWrite, error: _) if success { 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)') } //fails unless metadata is replaced with nil above }) } } }) 

错误是: Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn't be completed. (Cocoa error -1.)

元数据中的某些键似乎会导致失败。 这工作:

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

我有完全相同的问题,我已经提交了雷达rdar:// 21057247。 我尝试使用TSIlogging一个案例,但我被告知要提交一个雷达。

似乎填充PHContentEditingOutput对象的adjustementData属性是强制性的,以编辑照片。

我们应该把新图像的数据写到我们获得的URL上

  let theExportURL = output.renderedContentURL 

在我的情况下,我正在这样做…

  let outputData = UIImagePNGRepresentation(bakedImage) 

而在运行时,我得到了错误

 Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn't be completed. (Cocoa error -1.) 

但是当我开始导出这样的数据…

 let outputData = UIImageJPEGRepresentation(bakedImage, 1) 

有效。 这是logging在这里… https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHContentEditingOutput_Class/index.html

和这个相同的问题已经在这里回答了… https://stackoverflow.com/a/28362235

//如果资产不允许所请求的更改types,则这些方法将引发exception,在资产上调用canPerformEditOperation:以确定是否允许编辑操作的types。

  • (instancetype)changeRequestForAsset 🙁 PHAsset *)资产;

我想这是因为苹果不希望你改变metaData。你可以尝试下面的这个方法,也许创build一个新的是允许的。

  • (instancetype)creationRequestForAssetFromImage:(UIImage *)image;
  • (instancetype)creationRequestForAssetFromImageAtFileURL:(NSURL *)fileURL;
  • (instancetype)creationRequestForAssetFromVideoAtFileURL:(NSURL *)fileURL;