user4546067
user4546067

Reputation:

Blur an image via objective-c

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 !

Upvotes: 2

Views: 11736

Answers (5)

Daniel Ornelas
Daniel Ornelas

Reputation: 529

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

Upvotes: 29

Deepak Kumar
Deepak Kumar

Reputation: 1034

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

Upvotes: 2

Dheeraj D
Dheeraj D

Reputation: 4451

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

Upvotes: -1

Deepak Chaudhary
Deepak Chaudhary

Reputation: 1761

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

Upvotes: 3

Patrik Vaberer
Patrik Vaberer

Reputation: 666

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

Upvotes: 0

Related Questions