美文网首页roby的iOS开发文章收集iOSIOS核心动画学习
[iOS] 设置毛玻璃效果 (UIBlurEffect)与高斯模

[iOS] 设置毛玻璃效果 (UIBlurEffect)与高斯模

作者: 两年如歌 | 来源:发表于2016-03-31 11:09 被阅读6646次

iOS 8 后更新的API,使用 UIBlurEffect 类和 UIVisualEffectView 类添加毛玻璃特效更加便捷,高效。

//  创建显示图片
UIImageView *imageView = [[UIImageView alloc] init];

/**  毛玻璃特效类型
*  UIBlurEffectStyleExtraLight,
*  UIBlurEffectStyleLight,
*  UIBlurEffectStyleDark
*/
UIBlurEffect *blurEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleLight];

//  毛玻璃视图
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

//添加到要有毛玻璃特效的控件中
effectView.frame = imageView.bounds;
[imageView addSubview:effectView];

//设置模糊透明度
effectView.alpha = 0.5f;

iOS 7.0 的系统类

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、创建输入图像,CIImage类型,这里使用一个网上图片。
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、构建一个滤镜图表
CIColor *sepiaColor = [CIColor colorWithRed:0.76 green:0.65 blue:0.54];
// 2.1 先构建一个 CIColorMonochrome 滤镜,并配置输入图像与滤镜参数
CIFilter *monochromeFilter = [CIFilter filterWithName:@"CIColorMonochrome" withInputParameters:@{@"inputColor" : sepiaColor,@"inputIntensity":@1.0}];
[monochromeFilter setValue:inputImage forKey:@"inputImage"];// 通过KVC来设置输入图像
// 2.2 先构建一个 CIVignette 滤镜
CIFilter *vignetteFilter = [CIFilter filterWithName:@"CIVignette" withInputParameters:@{@"inputRadius" : @2.0,@"inputIntensity" :@1.0}];
[vignetteFilter setValue:monochromeFilter.outputImage forKey:@"inputImage"];// 以monochromeFilter的输出来作为输入
    
// 3、得到一个滤镜处理后的图片,并转换至 UIImage
// 创建一个 CIContext
CIContext *ciContext = [CIContext contextWithOptions:nil];
// 将 CIImage 过渡到 CGImageRef 类型
CGImageRef cgImage = [ciContext createCGImage:vignetteFilter.outputImage fromRect:inputImage.extent];
// 最后转换为 UIImage 类型
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
UIImageView *imgaeView = [[UIImageView alloc]initWithImage:uiImage];
imgaeView.frame = self.view.frame;
[self.view addSubview:imgaeView];

相关文章

网友评论

  • 清蒸鱼跃龙门:sepiaColor都导致图片变色了。。要求和原图一样的颜色怎么用第二种方法?

本文标题:[iOS] 设置毛玻璃效果 (UIBlurEffect)与高斯模

本文链接:https://www.haomeiwen.com/subject/tfpelttx.html