iOS 模糊效果

6 篇文章 0 订阅

       现在很多设计中都用到了模糊效果,或当背景,或实现渐变等等,模糊效果的应用范围已经变的越来越广泛。在这里介绍两种模糊效果,毛玻璃与高斯模糊。

       首先,介绍一种iOS 8 以后系统自带的毛玻璃效果。(顾名思义这种效果并不是对图片本身操作,所以使用要注意)

        self.imageV = [[UIImageView alloc] initWithFrame:self.view.frame];

        _imageV.image = [UIImage imageNamed:@"1.jpg"];

        _imageV.clipsToBounds = YES;

        _imageV.contentMode = UIViewContentModeScaleAspectFit;

        [self.view addSubview:_imageV];

        // 创建需要的毛玻璃特效类型
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        // 毛玻璃视图
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

        effectView.frame = _imageV.bounds;

        effectView.alpha = 0.8f;

        [_imageV addSubview:effectView];

      

       下面,使用CoreImage 来实现高斯模糊效果:

       CIContext *context = [CIContext contextWithOptions:nil];

       CIImage *inputImage = [CIImage imageWithCGImage:_imageV.image.CGImage];

       CGImageRef renderImage;

       CIImage *filtered = [inputImage imageByClampingToExtent];

       filtered = [filtered imageByApplyingFilter:@"CIGaussianBlur" withInputParameters:@{kCIInputRadiusKey:@20}];

       filtered = [filtered imageByCroppingToRect:inputImage.extent];

       renderImage = [context createCGImage:filtered fromRect:inputImage.extent];

       UIImage * blurImage = [UIImage imageWithCGImage:renderImage];

       _imageV.image = blurImage;

       其中,CIImage是CoreImage框架中最基本代表图像的对象,他不仅包含元图像数据,还包含作用在原图像上的滤镜链。这里我想特别强调的是CIImage和其他图像是不同的,在CIImage被CIContext渲染出来之前,他是依赖于滤镜链的,滤镜是不会更改CIImage中的图像数据。这个需要正确理解,不然会给你的程序造成错误。说到了CIImage的不同,就必须得提一下如何创建CIImage了,CIImage是不能直接有UIImage转化而来的。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值