高斯模糊滤镜逆转:iOS

时间:2013-01-25 05:42:00

标签: ios xcode uiview gaussian

我有一个高斯模糊,我正在为一个应用程序做。

    //Get a UIImage from the UIView
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Blur the UIImage
    CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
    CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [gaussianBlurFilter setValue:imageToBlur forKey:@"inputImage"];
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat:2] forKey:@"inputRadius"];
    CIImage *resultImage = [gaussianBlurFilter valueForKey:@"outputImage"];
    UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage];

    //Place the UIImage in a UIImageView
    newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    newView.image = endImage;
    [self.view addSubview:newView];

效果很好,但我希望能够将其反转并使视图恢复正常。

我怎么能这样做,因为试图简单地从它的超视图中删除模糊的视图不起作用。我也尝试过将各种属性设置为零而没有运气。

3 个答案:

答案 0 :(得分:3)

在属性中保留指向viewImage的指针

@property (nonatomic, strong) UIImage* originalImage;

之后

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

添加

self.originalImage = viewImage;

要还原您的图片:

newView.image = self.originalImage;

当你应用模糊时它不会改变viewImage ..你创建了一个单独的CIImage,它变得模糊,你从模糊的CIImage中创建一个新的UIImage。

答案 1 :(得分:1)

  • 我在您的代码中看到的第一件事是您不在异步任务中应用过滤器。模糊图像需要时间,所以如果你不想冻结主线程,你应该使用:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //blur the image in a second thread
    dispatch_async(dispatch_get_main_queue(), ^{
        //set the blurred image to your imageView in the main thread
    });
});

  • 要反转原始图像,只需将模糊副本放在原始图像上显示的其他图像视图中即可。在我的情况下,原始图像不是imageView,而是视图本身。所以我只在视图中添加一个imageView来设置模糊图像,当我想要反转时设置为nil。

  • 最后,如果您想在设置模糊图像时避免闪烁,可以在动画中使用alpha进行软转换:

[self.blurredImageView setAlpha: 0.0]; //make the imageView invisible
[self.blurredImageView setImage:blurredImage];
//and after set the image, make it visible slowly.
[UIView animateWithDuration:0.5 delay:0.1
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             [self.blurredImageView setAlpha: 1.0]; 
                         }
                         completion:nil];

  • 这是我的完整方法:

- (void)makeBlurredScreenShot{
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CIContext *context   = [CIContext contextWithOptions:nil];
    CIImage *sourceImage = [CIImage imageWithCGImage:imageView.CGImage];

    // Apply clamp filter:
    // this is needed because the CIGaussianBlur when applied makes
    // a trasparent border around the image
    NSString *clampFilterName = @"CIAffineClamp";
    CIFilter *clamp = [CIFilter filterWithName:clampFilterName];
    if (!clamp)
        return;

    [clamp setValue:sourceImage forKey:kCIInputImageKey];
    CIImage *clampResult = [clamp valueForKey:kCIOutputImageKey];

    // Apply Gaussian Blur filter
    NSString *gaussianBlurFilterName = @"CIGaussianBlur";
    CIFilter *gaussianBlur           = [CIFilter filterWithName:gaussianBlurFilterName];
    if (!gaussianBlur)
        return;

    [gaussianBlur setValue:clampResult forKey:kCIInputImageKey];
    [gaussianBlur setValue:[NSNumber numberWithFloat:8.0] forKey:@"inputRadius"];

    CIImage *gaussianBlurResult = [gaussianBlur valueForKey:kCIOutputImageKey];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        CGImageRef cgImage = [context createCGImage:gaussianBlurResult fromRect:[sourceImage extent]];

        UIImage *blurredImage = [UIImage imageWithCGImage:cgImage];
        CGImageRelease(cgImage);

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.blurredImageView setAlpha: 0.0];
            [self.blurredImageView setImage:blurredImage];
            [UIView animateWithDuration:0.5 delay:0.1
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                 [self.blurredImageView setAlpha: 1.0]; 
                             }
                             completion:nil];
        });
    });
}

- (void)removeBlurredScreenShot{
    [UIView animateWithDuration:0.5 delay:0.1
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                          [self.blurredImageView setAlpha: 0.0];
                             }
                     completion:^(BOOL finished) {
                          [self.blurredImageView setImage:nil];
                     }];
}

答案 2 :(得分:0)

就像我在之前的评论中所说,您可以创建一个属性/ iVar,在应用效果之前保存图像,并在您想要撤消时恢复。

if(!_originalImage)
    _originalImage = [[UIImage alloc] init];

_originalImage = viewImage; //(Creates a copy, not a C-Type pass-by-reference)

// Do your Blur Stuff

// Now somewhere down the line in your program, if you don't like the blur and the user would like to undo:

viewImage = _originalImage;

根据您对@HeWas的答案的评论,您不应该完全模糊视图。如果视图变得模糊,那么在程序的其他地方还有其他错误。

相关问题