我正在开发一个需要对整个UIView进行模糊处理的iPhone应用程序。我该如何实现这个功能?我看过了这个框架,但我不认为它适用于UIView。是否有其他方法可以对UIView进行模糊处理?
更新:请查看下面我更新的答案,添加了与iOS 7和iOS 8 的相关性。
我正在开发一个需要对整个UIView进行模糊处理的iPhone应用程序。我该如何实现这个功能?我看过了这个框架,但我不认为它适用于UIView。是否有其他方法可以对UIView进行模糊处理?
更新:请查看下面我更新的答案,添加了与iOS 7和iOS 8 的相关性。
这应该可以工作。我在代码中进行了注释,以帮助您理解正在发生的事情:
//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提供的提示)。这个问题中被接受的答案看起来很有前途,还有这个库。
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
我想发布这篇文章,因为您不再需要对单个帧或屏幕进行截图。UIView
,UIVisualEffectView
(https://developer.apple.com/documentation/uikit/uivisualeffectview)。UIVisualEffectView
来获取模糊的视图。
参见:https://developer.apple.com/documentation/uikit/uivisualeffectviewUIToolbar *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];
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;