iOS 8 后更新的API原探,使用 UIBlurEffect 類(lèi)和 UIVisualEffectView 類(lèi)添加毛玻璃特效更加便捷朗鸠,高效蚯撩。
// 創(chuàng)建顯示圖片
UIImageView *imageView = [[UIImageView alloc] init];
/** 毛玻璃特效類(lèi)型
* UIBlurEffectStyleExtraLight,
* UIBlurEffectStyleLight,
* UIBlurEffectStyleDark
*/
UIBlurEffect *blurEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleLight];
// 毛玻璃視圖
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
//添加到要有毛玻璃特效的控件中
effectView.frame = imageView.bounds;
[imageView addSubview:effectView];
//設(shè)置模糊透明度
effectView.alpha = 0.5f;
iOS 7.0 的系統(tǒng)類(lèi)
UIImageView *backView = [[UIImageView alloc] initWithFrame:self.view.bounds];
backView.image = [UIImage imageNamed:@"123.jpg"];
[self.view addSubview:backView];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.view.bounds];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[backView addSubview:toolbar];
toolbar.alpha = 0.5;
高斯模糊效果(濾鏡)
// 1、創(chuàng)建輸入圖像烛占,CIImage類(lèi)型胎挎,這里使用一個(gè)網(wǎng)上圖片。
CIImage *inputImage = [CIImage imageWithContentsOfURL:[NSURL URLWithString:@"http://echo-image.qiniucdn.com/FtPAdyCH-SlO-5xEe009AFE-N0EF?imageMogr2/auto-orient/quality/100%7CimageView2/4/w/640/q/100"]];
// 2忆家、構(gòu)建一個(gè)濾鏡圖表
CIColor *sepiaColor = [CIColor colorWithRed:0.76 green:0.65 blue:0.54];
// 2.1 先構(gòu)建一個(gè) CIColorMonochrome 濾鏡犹菇,并配置輸入圖像與濾鏡參數(shù)
CIFilter *monochromeFilter = [CIFilter filterWithName:@"CIColorMonochrome" withInputParameters:@{@"inputColor" : sepiaColor,@"inputIntensity":@1.0}];
[monochromeFilter setValue:inputImage forKey:@"inputImage"];// 通過(guò)KVC來(lái)設(shè)置輸入圖像
// 2.2 先構(gòu)建一個(gè) CIVignette 濾鏡
CIFilter *vignetteFilter = [CIFilter filterWithName:@"CIVignette" withInputParameters:@{@"inputRadius" : @2.0,@"inputIntensity" :@1.0}];
[vignetteFilter setValue:monochromeFilter.outputImage forKey:@"inputImage"];// 以monochromeFilter的輸出來(lái)作為輸入
// 3、得到一個(gè)濾鏡處理后的圖片芽卿,并轉(zhuǎn)換至 UIImage
// 創(chuàng)建一個(gè) CIContext
CIContext *ciContext = [CIContext contextWithOptions:nil];
// 將 CIImage 過(guò)渡到 CGImageRef 類(lèi)型
CGImageRef cgImage = [ciContext createCGImage:vignetteFilter.outputImage fromRect:inputImage.extent];
// 最后轉(zhuǎn)換為 UIImage 類(lèi)型
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
UIImageView *imgaeView = [[UIImageView alloc]initWithImage:uiImage];
imgaeView.frame = self.view.frame;
[self.view addSubview:imgaeView];