PHAssetChangeRequest 失败,除非原始图像方向为横向

Posted

技术标签:

【中文标题】PHAssetChangeRequest 失败,除非原始图像方向为横向【英文标题】:PHAssetChangeRequest fails unless original image orientation is landscape left 【发布时间】:2017-08-03 16:56:04 【问题描述】:

即使我根本不修改NSData,我的 PHAssetChangeRequest 也始终失败,并且当图像的方向不是横向左时,尝试在写入PHContentEditingOutputrenderedContentURL 时重用它 (1) .

如果还剩风景,我会收到请求许可的原生弹出窗口。但是,如果它是其他任何东西,它会默默地失败(错误代码-1),我知道这是一个方向问题的唯一方法是查看设备日志,我看到:

assetsd(Photos)[10493] <Notice>: Error: Invalid image orientation

这是我的代码:

 [lastImageAsset
      requestContentEditingInputWithOptions:options
                          completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) 
       NSURL *imageURL = contentEditingInput.fullSizeImageURL;
       NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

       PHContentEditingOutput *output = [[PHContentEditingOutput alloc]initWithContentEditingInput:contentEditingInput];
       PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc]initWithFormatIdentifier:@"Some Identifier" formatVersion:@"1.0" data:[@"Changed Something" dataUsingEncoding:NSUTF8StringEncoding]];
       [output setAdjustmentData:adjustmentData];
       [imageData writeToURL:output.renderedContentURL atomically:YES];

       [[phphotoLibrary sharedPhotoLibrary]performChanges:^
         PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest changeRequestForAsset:lastImageAsset];
         changeRequest.contentEditingOutput = output;
        completionHandler:^(BOOL success, NSError * _Nullable error) 
         NSLog(@"%@, %i", error, success);
       ];
  ];

我也试过了

CIImage *inputImage = [CIImage imageWithContentsOfURL:imageURL];
inputImage = [inputImage imageByApplyingOrientation:contentEditingInput.fullSizeImageOrientation];

感谢您的宝贵时间。

更新解决方案

这行得通:

 [lastImageAsset
      requestContentEditingInputWithOptions:options
                          completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) 

       NSURL *imageURL = contentEditingInput.fullSizeImageURL;

       CIImage *inputImage = [CIImage imageWithContentsOfURL:imageURL];
       inputImage = [inputImage imageByApplyingOrientation:contentEditingInput.fullSizeImageOrientation];

       EAGLContext *glContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
       CIContext *context = [CIContext contextWithEAGLContext:glContext];
       CGImageRef imageRef = [context createCGImage:inputImage fromRect:inputImage.extent];
       UIImage *image = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];

       NSData *imageData = UIImageJPEGRepresentation(image, 1.0);


       PHContentEditingOutput *output = [[PHContentEditingOutput alloc]initWithContentEditingInput:contentEditingInput];
       PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc]initWithFormatIdentifier:@"Some Identifier" formatVersion:@"1.0" data:[@"Changed Something" dataUsingEncoding:NSUTF8StringEncoding]];
       [output setAdjustmentData:adjustmentData];
       [imageData writeToURL:output.renderedContentURL atomically:YES];

       [[PHPhotoLibrary sharedPhotoLibrary]performChanges:^
         PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest changeRequestForAsset:lastImageAsset];
         changeRequest.contentEditingOutput = output;
        completionHandler:^(BOOL success, NSError * _Nullable error) 
         NSLog(@"%@, %i", error, success);
       ];
  ];

【问题讨论】:

【参考方案1】:

好的,终于找到了可行的方法:

CIImage *inputImage = [CIImage imageWithContentsOfURL:imageURL];
inputImage = [inputImage imageByApplyingOrientation:contentEditingInput.fullSizeImageOrientation];

EAGLContext *glContext = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
CIContext *context = [CIContext contextWithEAGLContext:glContext];
CGImageRef imageRef = [context createCGImage:inputImage fromRect:inputImage.extent];
UIImage *image = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

现在 imageData 写入并保持方向。特别感谢https://github.com/vandadnp/ios-8-Swift-Programming-Cookbook/blob/master/chapter-camera/Editing%20Images%20and%20Videos%20Right%20on%20the%20Device/Editing%20Images%20and%20Videos%20Right%20on%20the%20Device/ViewController.swift

【讨论】:

恭喜! :D .. 请用完整的解决方案更新您的问题,以供将来可能遇到相同问题的其他人使用 :) 当然是马西奥!并感谢您接受我的信息:)

以上是关于PHAssetChangeRequest 失败,除非原始图像方向为横向的主要内容,如果未能解决你的问题,请参考以下文章

PHAssetChangeRequest 以低分辨率保存

PHAssetChangeRequest 完成后 PHAsset 不反映更改

创建视频资产时,PHAssetChangeRequest 始终返回 nil

iOS 8 照片库 - 使用 PHAssetChangeRequest 添加资产时出错

std::string 擦除最后一个字符失败?

在 stm32 上擦除页面失败并显示 FLASH_ERROR_WRP