使用锐度滤镜后,Imageview未重置为原始

时间:2017-07-18 07:11:55

标签: ios objective-c uiimageview core-image luminance

在imageview上应用滤镜后无法获得原始图像。因为我希望在将滑块从1移动到0(最大值到最小值,我的意思是反方向)后获得原始图像。 以下是应用锐度效果的代码

- (IBAction)actionSharp:(UISlider *)sender {


    demoImage = _affectView.image;
    CIImage* image = [CIImage imageWithCGImage:demoImage.CGImage];
    CIContext *context = [CIContext contextWithOptions:nil];
    NSNumber *testNSNumber = [NSNumber numberWithFloat:_sharpActionWithSlider.value];
    NSLog(@"ffghhn %f",_sharpActionWithSlider.value);
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CISharpenLuminance" keysAndValues: @"inputImage", image, nil];

    [gaussianBlurFilter setDefaults];
    [gaussianBlurFilter setValue:testNSNumber forKey:kCIInputSharpnessKey];
    [gaussianBlurFilter setDefaults];
    CIImage *result2 = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result2 fromRect:[result2 extent]];
    UIImage *sharp= [UIImage imageWithCGImage:cgImage];
    UIImage *p = sharp;
    self.affectView.image= p;
    self.affectView.alpha = sender.value;
   CGImageRelease(cgImage);

}

2 个答案:

答案 0 :(得分:0)

您正在从_affectView获取演示图片,对其应用过滤器,然后再次将其保存在_affectView中。下次将_affectView检索到UIImage时,它将不是原始的,而是修改后的那个,因此回到原始状态是不可能的。

就在开始时(在此方法之外)将原始图片保存在demo中,并在每次需要时修改demo

答案 1 :(得分:0)

只需复制要添加过滤器的图像并将其保留在一旁。将效果添加到复制的图像并在_affectView图像视图中显示它们。

在每次从原始图像和效果中获取参考时,在滑块操作上,而不是从图像视图中获取图像。

将全局var创建为mainImage为

UIImage *mainImage;
在viewDidLoad中将原始图像分配给主图像

mainImage = [UIImage imagenamed:"yourMainImage"]; // assign from source local or url

在你的每个滑块动作的IBAction中,参考原始图像,而不是从imageview中获取图像。

- (IBAction)actionSharp:(UISlider *)sender {
UIImage *demoImage = mainImage;
CIImage* image = [CIImage imageWithCGImage:demoImage.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];
NSNumber *testNSNumber = [NSNumber numberWithFloat:_sharpActionWithSlider.value];
NSLog(@"ffghhn %f",_sharpActionWithSlider.value);
CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CISharpenLuminance" keysAndValues: @"inputImage", image, nil];

[gaussianBlurFilter setDefaults];
[gaussianBlurFilter setValue:testNSNumber forKey:kCIInputSharpnessKey];
[gaussianBlurFilter setDefaults];
CIImage *result2 = [gaussianBlurFilter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result2 fromRect:[result2 extent]];
UIImage *sharp= [UIImage imageWithCGImage:cgImage];
UIImage *p = sharp;
self.affectView.image= p;
self.affectView.alpha = sender.value;
CGImageRelease(cgImage);
}