如何对UIView应用模糊效果?

45

我正在开发一个需要对整个UIView进行模糊处理的iPhone应用程序。我该如何实现这个功能?我看过了这个框架,但我不认为它适用于UIView。是否有其他方法可以对UIView进行模糊处理?

更新:请查看下面我更新的答案,添加了与iOS 7和iOS 8 的相关性。

5个回答

47

这应该可以工作。我在代码中进行了注释,以帮助您理解正在发生的事情:

//To take advantage of CIFilters, you have to import the Core Image framework
#import <CoreImage/CoreImage.h>

//Get a UIImage from the UIView
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.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: 10] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"]; 
UIImage *endImage = [[UIImage alloc] initWithCIImage:resultImage];

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

如果你对代码有任何问题,请在评论中留言。

注意:自iOS 5.1起,CIGaussianBlur不可用,因此您必须找到另一种方法来为iOS 5.x+设备模糊视图(感谢@BradLarson提供的提示)。这个问题中被接受的答案看起来很有前途,还有这个库


10
请注意,截至5.1版本,iOS上没有CIGaussianBlur,因此您可能需要找到其他方法来在5.x及更早版本中实现此效果。 - Brad Larson
你在这段代码中看到了self.view和myView之间的模糊吗? - Steven Fisher
但是它的行为就像是在视图周围切割了一个区域。 - hasan
所以我使用断点检查了此代码中的 CIImage 和 UIImage 对象,并发现模糊图像在 CIImage 对象中显示正常,但 UIImage 对象没有显示任何内容。当点击该对象上的眼睛按钮时,它会显示“无法加载 endImage 的快速加载数据”。 - genaks
我有同样的问题。 - Hobbes the Tige
显示剩余2条评论

29
自从iOS 7发布以来,这个问题一直备受关注,我想我应该跟进一下我最终做了什么。我当时用Photoshop模糊了图片,但是还有许多很棒的第三方库可以进行动态和静态模糊,以下是其中几个:
- 这个巧妙的库从UITabBar中取出一个图层并应用到任何视图上,以获取iOS 7的本机模糊效果: https://github.com/JagCesar/iOS-blur/ - 这个库是一个实时模糊库,可以对其下面的任何视图进行实时模糊: https://github.com/alexdrone/ios-realtimeblur(由qegal指出) - 另一个可以进行相同实时模糊的库: https://github.com/nicklockwood/FXBlurView - 这个库是我个人使用的一个非常好的库,允许您设置色调颜色等其他设置: https://github.com/ivoleko/ILTranslucentView 我想发布这篇文章,因为您不再需要对单个帧或屏幕进行截图。
iOS 8:随着iOS 8的即将发布,Apple已经包含了内置的模糊UIViewUIVisualEffectView(https://developer.apple.com/documentation/uikit/uivisualeffectview)。

FXBlurView适用于iOS5+ https://github.com/nicklockwood/FXBlurView。 - lucasart
请注意,至少有一个应用程序因使用这些库(iOS-blur https://github.com/JagCesar/iOS-blur/issues/25)而被拒绝。这些库使用`UITabBar`内部的一个层来实现效果,而苹果似乎正在拒绝这些库。 - pgb
1
这个问题似乎已经在新的提交中得到了修复。而且被拒绝的应用程序是因为另一个(主要)原因被拒绝的。 - virindh

13

如果您设置了一个UIVisualEffectView,然后更改了效果,如何重新加载该视图? - Ace Green
我认为在改变效果时重新加载它是不可能的 - 你必须重新初始化它。 - Robert

1
“只需使用此代码即可简单解决。”
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:yourView.bounds];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 7.0) {
    toolBar.barTintColor = nil;
    toolBar.translucent = YES;
    toolBar.barStyle = UIBarStyleBlack;
}
else
    [toolBar setTintColor:[UIColor colorWithRed:5 green:31 blue:75 alpha:1]];
[yourView insertSubview:toolBar atIndex:0];

0
最简单的模糊视图的方法是添加UIToolbar,只需更改alpha值即可改变模糊程度。
self.toolbar = [[UIToolbar alloc]initForAutoLayout];
[self.view addSubview:self.toolbar];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:NAVBAR_HEIGHT];
[self.toolbar autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0];
self.toolbar.alpha = 0.5;

这在被接受的答案中已经讨论过了。然而,随着iOS 8和UIVisualEffect类和API的推出,这种方法不再高效(或建议使用)。 - virindh
我们能否在iOS7中使用UIToolbar作为模糊视图,而不使用任何第三方库? - yoshiiiiiiii

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接