"orientation = 6" 已元数据编辑到相机胶卷的水平方向无法保存

The horizontal orientation of "orientation = 6" that has been metadata-edited to the camera roll can not be saved

我正在尝试编辑相机胶卷照片的元数据。

但是,如果您在编辑元数据后尝试覆盖照片,总是会出现以下错误:

'Error Domain=NSCocoaErrorDomain Code=-1 "操作无法完成。(Cocoa错误-1。) '

这发生在风景图片(方向标签为“6”)等照片中。

对于肖像图像(方向标签为“1”),可以正确覆盖和保存照片。

如果使用UIImageJPEGRepresentation (image, 1.0)解码,横向图片可以被覆盖保存。

但我不想降低图像质量,所以我正在寻找另一种方法。

重现步骤:

  1. 将相机胶卷的图片保存在'Documents/..

  2. 编辑一张照片的元数据。 (从现在开始,在 Documents / 下)

  3. 获取资产"requestContentEditingInputWithOptions ()".

  4. 获取'Step2'照片的URL,获取CGImageSourceRef与'CGImageSourceCreateWithURL ()'

  5. 通过指定 contentEditingOutput.renderedContentURL 获取 CGImageDestinationRef 'CGImageDestinationCreateWithURL ()'.

  6. 将 CGImageSourceRef、CGImageDestinationRef 和元数据设置为 CGImageDestinationAddImageFromSource。

  7. 使用 CGImageDestinationFinalize 保存 CGImageDestinationRef。

  8. 创建 PHA 调整数据。将元数据设置为 'data'.

  9. 在setAdjustmentData中设置AdjustmentData

  10. 在PHPhotoLibrary的changeRequestForAsset中设置集合。 → 'PHAssetChangeRequest * 请求 = [PHAssetChangeRequest changeRequestForAsset: 资产];'

  11. 'request.contentEditingOutput = contentEditingOutput;'

  12. 'completion' 是一个错误。出现以下错误:『Error Domain=NSCocoaErrorDomain Code=-1 "The operation couldn't be 完全的。 (Cocoa错误-1。)'

代码如下:

    - (void)replaceMetadataIntoPhoto:(NSInteger)index metadata:(NSDictionary *)metadata 
    { 
        PHAsset *asset = _assetsList[index]; 
   
        [asset requestContentEditingInputWithOptions:nil 
                                   completionHandler:^(PHContentEditingInput *_Nullable contentEditingInput, NSDictionary *_Nonnull info) { 
   
                                       NSURL *url = [contentEditingInput fullSizeImageURL]; 
                                       CGImageSourceRef sourceImage = CGImageSourceCreateWithURL((__bridge CFURLRef)url, nil); 
   
                                       PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput]; 
   
                                       CGImageDestinationRef outputDestination = CGImageDestinationCreateWithURL((__bridge CFURLRef)contentEditingOutput.renderedContentURL, CGImageSourceGetType(sourceImage), 1, NULL); 
   
                                       CGImageDestinationAddImageFromSource(outputDestination, sourceImage, 0, (__bridge CFDictionaryRef)metadata); 
   
                                       CGImageDestinationFinalize(outputDestination); 
                                       CFRelease(sourceImage); 
                                       CFRelease(outputDestination); 
   
                                       PHAdjustmentData *adjustmentData = 
                                       [[PHAdjustmentData alloc] initWithFormatIdentifier:@"hogehoge" 
                                                                            formatVersion:@"1.0" 
                                                                                     data:[NSKeyedArchiver archivedDataWithRootObject:@{@"metadata": metadata}]]; 
   
                                       [contentEditingOutput setAdjustmentData:adjustmentData]; 
   
                                       [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 
                                           PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset]; 
                                           request.contentEditingOutput = contentEditingOutput; 
   
                                       }   completionHandler:^(BOOL success, NSError *error) { 
                                             if (error) { 
                                                  DBGLog(@"error=%@", error); 
                                              } 
   
                                       }]; 

预期结果:

能够在相机胶卷上使用编辑后的元数据覆盖和保存风景图像。

实际结果:

覆盖保存失败

我从Apple的反馈助手那里得到了答案。 以下是引用。

Please refer to https://developer.apple.com/documentation/photokit/phcontenteditingoutput/1518655-renderedcontenturl Important Edited asset content must incorporate (or “bake in”) the intended orientation of the asset. That is, the orientation metadata (if any) that you write in the output image or video file must declare the “up” orientation, and the image or video data must appear right-side up when presented without orientation metadata. If you want to rewrite orientation metadata in the EXIF or otherwise, you should try creating a new asset, rather than using PHContentEditingOutput.