我PHAssetChangeRequest始终失败,即使我不修改NSData
所有,也试着写的时候再使用它PHContentEditingOutput
的renderedContentURL
当图像的方向是什么,比其他景观左(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);
}];
}];