Gaussian Filter Blur Filter: iOS

I have a Gaussian spot that I am making for an application.

    //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 fine, but I want to be able to undo it and return the view to normal.

How can I do this, trying to simply remove the blur form from it, the supervisor does not work. I also tried to set various properties to zero, no luck.

+5
source share
3 answers

Hold the pointer to viewImage in the property

@property (nonatomic, strong) UIImage* originalImage;

then after

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

add

self.originalImage = viewImage;

To return an image:

newView.image = self.originalImage;

when you apply blur, it does not change the viewImage .. you have created a separate CIImage that becomes blurred and you create a new UIImage from the blurred CIImage.

+3
source
  • , , , async. , , , :

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
    });
});

  • , , . - , . ViewView, , nil, .

  • , , , :

[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];
                     }];
}
+1

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

According to your comment on @HeWas answer, you shouldn't completely blur the view. If the view becomes blurry, you are doing something else wrong elsewhere in your program.

0
source

All Articles