關(guān)于高斯模糊先來看看iOS7的實現(xiàn)吧
1.利用toolbar
UIImageView*backView = [[UIImageViewalloc] initWithFrame:self.view.bounds];
backView.image= [UIImageimageNamed:@"123.jpg"];
[self.viewaddSubview:backView];
UIToolbar*toolbar = [[UIToolbaralloc] initWithFrame:self.view.bounds];
toolbar.barStyle=UIBarStyleBlackTranslucent;
[backView addSubview:toolbar];toolbar.alpha=0.5;
2.使用濾鏡
// 1钧舌、創(chuàng)建輸入圖像比吭,CIImage類型,這里使用一個網(wǎng)上圖片兼丰。
CIImage*inputImage = [CIImageimageWithContentsOfURL:[NSURLURLWithString:@"http://echo-image.qiniucdn.com/FtPAdyCH-SlO-5xEe009AFE-N0EF?imageMogr2/auto-orient/quality/100%7CimageView2/4/w/640/q/100"]];
// 2炬搭、構(gòu)建一個濾鏡圖表
CIColor*sepiaColor = [CIColorcolorWithRed:0.76green:0.65blue:0.54];
// 2.1 先構(gòu)建一個 CIColorMonochrome 濾鏡漏益,并配置輸入圖像與濾鏡參數(shù)CIFilter*monochromeFilter =[CIFilterfilterWithName:@"CIColorMonochrome"withInputParameters:@{@"inputColor": sepiaColor,@"inputIntensity":@1.0}];
[monochromeFilter setValue:inputImage forKey:@"inputImage"];
// 通過KVC來設置輸入圖像
// 2.2 先構(gòu)建一個 CIVignette 濾鏡
CIFilter*vignetteFilter = [CIFilterfilterWithName:@"CIVignette"withInputParameters:@{@"inputRadius": @2.0,@"inputIntensity":@1.0}];
[vignetteFilter setValue:monochromeFilter.outputImageforKey:@"inputImage"];
// 以monochromeFilter的輸出來作為輸入
// 3、得到一個濾鏡處理后的圖片粘舟,并轉(zhuǎn)換至 UIImage
// 創(chuàng)建一個 CIContextCIContext*ciContext = [CIContextcontextWithOptions:nil];
// 將 CIImage 過渡到 CGImageRef 類型
CGImageRefcgImage = [ciContext createCGImage:vignetteFilter.outputImagefromRect:inputImage.extent];
// 最后轉(zhuǎn)換為 UIImage 類型UIImage*uiImage = [UIImageimageWithCGImage:cgImage];
UIImageView*imgaeView = [[UIImageViewalloc]initWithImage:uiImage];
imgaeView.frame=self.view.frame;[self.viewaddSubview:imgaeView];
3.使用UIVisualEffect 是iOS8中的類
但經(jīng)常地熔脂,我們需要在模糊和透明兩個狀態(tài)之間切換,通常大家的做法就是直接動畫它的alpha值柑肴,但是...結(jié)果不言而喻锤悄,漸變的過程十分奇怪,有點朦朧的感覺....總之就是不好看嘉抒,并且 iOS 也會 print 出一個警告說效果在alpha為 1 之前會 broken零聚。好吧,總之漸變 alpha 值的做法大家以后一定不要用了些侍。
然后隶症,很多人就想到要自己實現(xiàn) Blur 算法,達到自定義的效果岗宣,也有用 Private API 來強行設置UIBlurEffect的blurRadius屬性蚂会,但這些都不是最好的方法
其實,iOS 內(nèi)部早已提供了一個完美的解決方案耗式,那就是UIView.animateWith...胁住,使用這個方法可以完美地漸變模糊半徑和 Vibrancy 亮度效果,如果大家仔細看了WWDC 2015的What's New in Cocoa Touch這個 Session 的話刊咳,你應該聽說了 iOS 9 新增了模糊動畫的功能彪见。
看看用法吧
//? 創(chuàng)建顯示圖片
UIImageView* imageView = [[UIImageViewalloc] init];
/**? 毛玻璃特效類型
*? UIBlurEffectStyleExtraLight,
*? UIBlurEffectStyleLight,
*? UIBlurEffectStyleDark
*/
UIBlurEffect* blurEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleLight];
//? 毛玻璃視圖
UIVisualEffectView* effectView = [[UIVisualEffectViewalloc] initWithEffect:blurEffect];
//添加到要有毛玻璃特效的控件中
effectView.frame= imageView.bounds;[imageView addSubview:effectView];
//設置模糊透明度
effectView.alpha=.5f;