2

I want to blur an UIImage for an app, Does any of you have an Objective-C method to blur an image ?

I tried to find a method or a technique but couldn't find anything. Help please !

5 Answers5

29

You can use core image filters. http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html

Look at this snippet from https://gist.github.com/betzerra/5988604

//  Needs CoreImage.framework

- (UIImage *)blurredImageWithImage:(UIImage *)sourceImage{

    //  Create our blurred image
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];

    //  Setting up Gaussian Blur
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:15.0f] forKey:@"inputRadius"];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];

    /*  CIGaussianBlur has a tendency to shrink the image a little, this ensures it matches 
     *  up exactly to the bounds of our original image */
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

    UIImage *retVal = [UIImage imageWithCGImage:cgImage];

    if (cgImage) {
        CGImageRelease(cgImage);
    }

    return retVal;
}
Vincent Sit
  • 2,214
  • 1
  • 24
  • 27
Daniel Ornelas
  • 439
  • 4
  • 6
3

You can use UIVisualEffectView with visual effects. Initialize an element of visualEffect and effectView than add to your view or imgview wherever you want :). Also you can choose EffectStyles. code snippet :

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];

visualEffectView.frame = YourImgView.bounds;
[YourImgView addSubview:visualEffectView];
Deepak Chaudhary
  • 1,723
  • 1
  • 15
  • 29
2

Solution :

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurEffectView.frame = self.view.bounds;
    blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    //ADD BLUR EFFECT VIEW IN MAIN VIEW
    [self.view addSubview:blurEffectView];
Deepak Kumar
  • 1,035
  • 10
  • 18
0

I recommend to use Storyboard for implementing blur or vibrancy by UIVisualEffectView. For more, look at my sample project at https://github.com/Vaberer/BlurTransition to demonstrate how to it works and how use it with autolayout inside the UIVisualEffect

Patrik Vaberer
  • 646
  • 6
  • 15
-1

Swift 2.2 :

 func blurredImage(with sourceImage: UIImage) -> UIImage {


        let filter = CIFilter(name: "CIGaussianBlur")
        filter!.setValue(CIImage(image: sourceImage), forKey: kCIInputImageKey)
        filter!.setValue(0.8, forKey: kCIInputIntensityKey)
        let ctx = CIContext(options:nil)
        let cgImage = ctx.createCGImage(filter!.outputImage!, fromRect:filter!.outputImage!.extent)


        return UIImage(CGImage:cgImage)
}
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34