除非原始图像方向是横向左PHAssetChangeRequest失败(PHAssetChangeRe

2019-10-29 21:53发布

我PHAssetChangeRequest始终失败,即使我不修改NSData所有,也试着写的时候再使用它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];

谢谢你的时间。

与Solution UPDATE

这工作:

 [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);
       }];
  }];

Answer 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 %20于是%20Videos%20Right%20on%第二十条%20Device / ViewController.swift



文章来源: PHAssetChangeRequest fails unless original image orientation is landscape left