How does the blur effect apply to UIView in iOS?

In my application, I want to apply a blur effect on uiview.So, how can I achieve a blur effect. I tried the code below:

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 the problem is using this code. when the blur effect is applied, the viewing time is short.

+8
ios objective-c uiview blur uiblureffect
source share
2 answers

Just put this blur into the view (here yourBlurredView) that you want to blur. 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) 
+11
source share

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

+2
source share

All Articles