2012-06-21 20 views
43

现在我正在构建一个需要模糊整个UIView的iPhone应用程序。我怎样才能做到这一点?我看过this framework,但我不认为它适用于UIView。有没有一种替代方法来模糊UIView?如何将模糊应用到UIView?

UPDATE:检查下面我更新的答案,与iOS 7和iOS 8

回答

27

自从iOS 7发布以来,这个问题已经得到很多点击,我认为我应该跟进我最终做的事情。

我个人画面模糊,在用photoshop的时间,但也有那些动态静态模糊许多伟大的第三方库,这里有一对夫妇:

我想后,是因为哟你不再需要需要截取个别框架或屏幕截图。

的iOS 8:与iOS 8的到来发布,苹果已经包含在模糊UIViewUIVisualEffectViewhttps://developer.apple.com/documentation/uikit/uivisualeffectview)内置

+0

FXBlurView适用于iOS5 + [https://github.com/nicklockwood/FXBlurView](https://github.com/nicklockwood/FXBlurView)。 –

+0

请注意,至少有一个应用程序因使用其中一些库而被拒绝(iOS-blur https://github.com/JagCesar/iOS-blur/issues/25)。这些库在'UITabBar'中使用一个图层来实现效果,而Apple似乎拒绝这些。 – pgb

+1

这似乎已经用新的提交修复了。另外,被拒绝的应用程序因另一个(主要)原因而被拒绝。 – virindh

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

如果您对代码有任何疑问,请将其留在注释中。

注意: CIGaussianBlur在5.1版本的iOS中不存在,因此您必须找到另一种方法来模糊设备5.x +的视图(感谢此提示的@BradLarson)。 this question中的接受答案看起来很有前途,可以替代,this library也是如此。

+10

请注意,CIGaussianBlur在5.1版本的iOS上不存在,所以您可能需要在5.x和更旧的版本上找到完成此操作的不同方法。 –

+0

你在这段代码中得到了self.view和myView之间的模糊,对吧? –

+0

嗨@qegal,我如何改变模糊效果的强度? –

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; 
+0

这在接受的答案中讨论。然而,随着iOS 8的推出以及UIVisualEffect类和API的 – virindh

+0

的推出,这已经不再有效(或建议)。因此,我们可以将UIToolbar作为ios7的模糊视图,而不使用任何第三方库。 – YaBoiSandeep

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