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



即使我根本不修改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);
}];
}];

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

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

最新更新