基于SamplePhotosApp的代码:
- (void)applyFilterWithName:(NSString *)filterName {
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
[options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
return [adjustmentData.formatIdentifier isEqualToString:AdjustmentFormatIdentifier] && [adjustmentData.formatVersion isEqualToString:@"1.0"];
}];
[self.asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *url = [contentEditingInput fullSizeImageURL];
int orientation = [contentEditingInput fullSizeImageOrientation];
CIImage *inputImage = [CIImage imageWithContentsOfURL:url options:nil];
inputImage = [inputImage imageByApplyingOrientation:orientation];
// Add filter
CIFilter *filter = [CIFilter filterWithName:filterName];
[filter setDefaults];
[filter setValue:inputImage forKey:kCIInputImageKey];
CIImage *outputImage = [filter outputImage];
// Create editing output
NSData *jpegData = [outputImage aapl_jpegRepresentationWithCompressionQuality:0.9f];
PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:AdjustmentFormatIdentifier formatVersion:@"1.0" data:[filterName dataUsingEncoding:NSUTF8StringEncoding]];
PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];
[jpegData writeToURL:[contentEditingOutput renderedContentURL] atomically:YES];
[contentEditingOutput setAdjustmentData:adjustmentData];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:self.asset];
request.contentEditingOutput = contentEditingOutput;
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error: %@", error);
}
}];
}];
}
我创建了自己的应用过滤器到图像的实现。我的代码:
- (void)saveAsset:(PHAsset *)asset withEdits:(NSDictionary *)edits withCompletion:(successBlock)block {
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
[options setCanHandleAdjustmentData:^BOOL(PHAdjustmentData *adjustmentData) {
return [adjustmentData.formatIdentifier isEqualToString:@"myID"] && [adjustmentData.formatVersion isEqualToString:@"1.0"];
}];
[asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *fullImageURL = [contentEditingInput fullSizeImageURL];
UIImage *fullSizeImage = [UIImage imageWithContentsOfFile:[fullImageURL path]];
UIImage *editedImage = [EngineImageFilters applyEffects:edits[@"filters"] toImages:@[fullSizeImage] withValue:@(ImageFilterValueTypeCustom)][0];
NSData *imageData = UIImageJPEGRepresentation(editedImage, 1.0);
NSData *editData = [NSJSONSerialization dataWithJSONObject:edits options:0 error:nil];
PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"profcam" formatVersion:@"1.0" data:editData];
PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:contentEditingInput];
[imageData writeToURL:[output renderedContentURL] atomically:YES];
[output setAdjustmentData:adjustmentData];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:asset];
request.contentEditingOutput = output;
} completionHandler:^(BOOL success, NSError *error) {
if (block) {
block(success, error);
}
}];
}];
}
由于许多值为零,我的片段会导致崩溃。 requestContentEditingWithOptions:completionHandler方法为PHContentEditingInput提供nil,这会导致许多问题,例如文件url为nil等等。似乎从未调用setCanHandleAdjustmentData块 - 也许这就是PHContentEditingInput为零的原因。无论如何,我该如何解决这个问题?