Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Blur effect apply on UIView in iOS?

In my application i want to apply blur effect on uiview.So how can i achive blur effect. I tried by below code:

UIGraphicsBeginImageContext(scrollview.bounds.size);
[scrollview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Blur the UIImage with a CIFilter
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat:3] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage];

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:scrollview.bounds];
newView.image = endImage;
[scrollview addSubview:newView];

But having problem using this code. when apply blur effect that time view became small.

like image 534
Monika Patel Avatar asked Sep 03 '15 02:09

Monika Patel


2 Answers

Just put this blur view on the View (here yourBlurredView) which you want to be blurred. Here is an example in Objective-C:

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

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

visualEffectView.frame = yourBlurredView.bounds; 
[yourBlurredView addSubview:visualEffectView];

and Swift:

var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))    

visualEffectView.frame = yourBlurredView.bounds

yourBlurredView.addSubview(visualEffectView)
like image 120
Vineet Ashtekar Avatar answered Oct 21 '22 10:10

Vineet Ashtekar


If you are working with iOS 8 or later, try using a UIVisualEffectView with a UIBlurEffect.

like image 32
WolfLink Avatar answered Oct 21 '22 10:10

WolfLink