2012-06-21 21 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];