Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gaussian Blur Filter Reversal: iOS

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.

like image 349
jakenberg Avatar asked Oct 05 '22 06:10

jakenberg


1 Answers

Keep a pointer to viewImage in a property

@property (nonatomic, strong) UIImage* originalImage;

then after

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

add

self.originalImage = viewImage;

To revert your image:

newView.image = self.originalImage;

when you apply the blur it does not alter viewImage.. you have created a separate CIImage which gets blurred, and you make a new UIImage from the blurred CIImage.

like image 72
foundry Avatar answered Oct 10 '22 01:10

foundry