5

I've got a gaussian blur which I'm doing for an app.

    //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];

It works great, but I want to be able to reverse it and return the view back to normal.

How can I do this as trying to simply remove the blur's view from it's superview didn't work. I've also tried setting various properties to nil with no luck.

4

3 回答 3

3

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

@property (nonatomic, strong) UIImage* originalImage;

然后

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

添加

self.originalImage = viewImage;

要恢复您的图像:

newView.image = self.originalImage;

当您应用模糊时,它不会改变 viewImage.. 您已经创建了一个单独的 CIImage,该 CIImage 变得模糊,并且您从模糊的 CIImage 制作了一个新的 UIImage。

于 2013-01-25T06:17:10.057 回答
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,而是视图本身。所以我只在我的视图中添加一个 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];
                     }];
}
于 2014-04-11T08:10:14.040 回答
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 回答的评论,您不应该完全模糊视图。如果视图变得模糊,则说明您在程序的其他地方做错了其他事情。

于 2013-01-25T16:40:55.233 回答