如何模糊效果适用于iOS中的UIView?

时间:2015-09-03 02:52:12

标签: ios objective-c uiview blur uiblureffect

在我的应用程序中,我想在uiview上应用模糊效果。所以我怎样才能实现模糊效果。 我试过下面的代码:

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

但是使用此代码时出现问题。当应用模糊效果时,时间视图变小。

2 个答案:

答案 0 :(得分:27)

只需将此模糊视图放在要模糊的视图(此处为yourBlurredView)上即可。这是Objective-C中的一个例子:

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

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

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

和斯威夫特:

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

visualEffectView.frame = yourBlurredView.bounds

yourBlurredView.addSubview(visualEffectView)

答案 1 :(得分:2)

如果您使用的是iOS 8或更高版本,请尝试使用带有UIVisualEffectViewUIBlurEffect

相关问题