Reputation:
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
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
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
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
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
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