除非原始图像方向是横向左侧,否则PHAssetChangeRequest将失败

时间:2017-08-03 16:56:04

标签: ios objective-c nsdata phasset ciimage

即使我根本没有修改NSData,我的PHAssetChangeRequest也会始终如一地失败,并且在写入PHContentEditingOutput的{​​{1}}时尝试重用它,此时图像的方向是其他任何其他内容比左边的景观(1)。

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

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

1 个答案:

答案 0 :(得分:0)

好的,终于找到了有用的东西:

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