首先,我问办公室的老人甚至老板,但他们没有回复我的问题。 - 如何在同一时间使用两个过滤器保存一张照片?
我有一个使用PHAssets编辑资源元数据(照片/视频)的项目。现在,我可以通过应用标签和过滤器来修改图像。请参阅此示例:http://dev.classmethod.jp/references/ios8-photo-kit-4/
我最近有一个关于CIGaussianBlur生成的额外边框的问题,我了解到我必须使用CIAffineClamp过滤器来解决问题:CIGaussianBlur and CIAffineClamp on iOS 6
不幸的是,当我需要应用两个过滤器时,我不知道如何保存PHAsset或修改PHAsset。这是我应用这两个过滤器的代码:
// G: If Blur, then adjust first the frame before applying blur!
if ([filterName isEqualToString:@"CIGaussianBlur"])
{
[filter setValue:inputImageForFilter forKey:@"inputImage"];
CGFloat blurLevel = 20.0f;
[filter setValue:[NSNumber numberWithFloat:blurLevel] forKey:@"inputRadius"];
CIImage* filterInputImage = [CIImage imageWithCGImage:originalImage.CGImage];
CIFilter* filter = [CIFilter filterWithName:filterName];
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
[clampFilter setDefaults];
[clampFilter setValue:filterInputImage forKey:kCIInputImageKey];
[filter setValue:clampFilter.outputImage forKey:kCIInputImageKey];
[filter setValue:@10.0f forKey:@"inputRadius"];
CIImage* filterOutputImage = [filter valueForKey:kCIOutputImageKey];
CGImageRef createdImage = [context createCGImage:filterOutputImage fromRect:[filterInputImage extent]];
UIImage* outputImage = [UIImage imageWithCGImage:createdImage];
dispatch_async(dispatch_get_main_queue(), ^{
strongSelf.capturedImageView.image = outputImage;
strongSelf.capturedImageView.contentMode = UIViewContentModeScaleAspectFit;
[strongSelf.capturedImageView layoutSubviews];
});
strongSelf.appliedFilter.filterName = filterName;
strongSelf.appliedFilter.editingInput = contentEditingInput;
strongSelf.appliedFilter.outputImage = filterOutputImage;
CGImageRelease(createdImage);
createdImage = nil;
}
这是保存数据的代码:
- (void)doneButtonAction:(id)sender
{
// G: Handle Nil FilterName for Fixing Crash :)
if (self.appliedFilter.filterName == nil) {
// G: just pop to the view controller next to root, which is the Camera View Controller in Photo Mode.
[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}
else{
// Create a PHAdjustmentData object that describes the filter that was applied.
PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:AdjustmentFormatIdentifier formatVersion:@"1.0" data:[self.appliedFilter.filterName dataUsingEncoding:NSUTF8StringEncoding]];
PHAdjustmentData *affineClamp = [[PHAdjustmentData alloc] initWithFormatIdentifier:AdjustmentFormatIdentifier formatVersion:@"1.0" data:[@"CIAffineClamp" dataUsingEncoding:NSUTF8StringEncoding]];
/*
Create a PHContentEditingOutput object and write a JPEG representation
of the filtered object to the renderedContentURL.
*/
PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.appliedFilter.editingInput];
NSData *jpegData = [self.appliedFilter.outputImage aapl_jpegRepresentationWithCompressionQuality:0.9f];
[jpegData writeToURL:[contentEditingOutput renderedContentURL] atomically:YES];
[contentEditingOutput setAdjustmentData:adjustmentData];
if ([self.appliedFilter.filterName isEqualToString:@"CIGaussianBlur"]) {
[contentEditingOutput setAdjustmentData:affineClamp];
}
// Ask the shared PHPhotoLinrary to perform the changes.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:self.photo.asset];
request.contentEditingOutput = contentEditingOutput;
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error: %@", error);
}
}];
// G: just pop to the view controller next to root, which is the Camera View Controller in Photo Mode.
[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}
}
同样,如果我只有1个过滤器可以应用,那么保存数据部分代码的效果非常好。所以,我的问题是如何在有两个要应用的过滤器时调整数据。非常感谢。我相信我的问题很清楚。 ;)期待你的回答。
答案 0 :(得分:0)
首先,我不知道如何重新声明*过滤而不会出现错误(必须首先在控制结构之外声明,然后在其中重新声明)。
这是一个简单的例程,它将应用您的模糊并返回包含您正在寻找的范围的图像。
- (CIImage*)blurImage:(CIImage*)sourceImage withRadius:(NSNumber*)blurRadius {
CGRect originalExtent = sourceImage.extent;
CIImage *clampedImaged = [sourceImage imageByClampingToExtent];
CIImage *blurredImage = [clampedImaged imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{@"inputRadius" : blurRadius}];
return [blurredImage imageByCroppingToRect:originalExtent];
}
否则,我无法告诉你originalImage或inputImageForFilter指的是什么,所以很难说出挂断的位置。你可以逐行调试这个并使用Xcode的peek函数(eyeball图标)来确保从每个过滤器生成的CIImage对象是你期望的。