• iOS毛玻璃效果的实现方法


    ios开发中常常用到的毛玻璃效果实现方法

    iOS8以后使用系统里的UIBlurEffect可以实现,UIBlurEffect继承自UIVisualEffect

    UIBlurEffectStyle有三个值,UIBlurEffectStyleLight , UIBlurEffectStyleExtraLight , UIBlurEffectStyleDark,可以控制毛玻璃的效果.

        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    //必须给effcetView的frame赋值,因为UIVisualEffectView是一个加到UIIamgeView上的子视图.
        effectView.frame = _imageView.bounds;
        [self.imageView addSubview:effectView];
    

    UIVibrancyEffect也继承自UIVisualEffect类,可以用来设置一些特殊的效果.代码如下

        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
        effectView.frame = _imageView.bounds;
        
        UIVibrancyEffect *viBrancyeffect = [UIVibrancyEffect effectForBlurEffect:effect];
        UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:viBrancyeffect];
        
        vibrancyEffectView.frame = CGRectMake(100, 0, 200, 200);
        UILabel *lbl = [[UILabel alloc] init];
        lbl.text = @"测试Label";
        lbl.font = [UIFont systemFontOfSize:25];
        lbl.frame = CGRectMake(0, 0, 200, 200);
     
        [vibrancyEffectView.contentView addSubview:lbl];
        [effectView.contentView addSubview:vibrancyEffectView];
        [self.imageView addSubview:effectView];
    

    但是这种毛玻璃效果不能很好的控制模糊效果,可以alpha属性控制并不完美.
    效果如下:


    使用系统CoreImage中的滤镜产生毛玻璃效果

    原理是给图片添加滤镜,这种方式相比上面更为可控,下面介绍一下系统滤镜中支持的毛玻璃效果

    先简要介绍一下系统滤镜CIFilter的使用

    CIfilter中有一个专门用于毛玻璃效果的Category : kCICategoryBlur
    使用下面的代码可以打印出这个分类下的滤镜

    NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBlur];
    

    可以得到结果

    **    CIBoxBlur,**
    **    CIDiscBlur,**
    **    CIGaussianBlur,**
    **    CIMaskedVariableBlur,**
    **    CIMedianFilter,**
    **    CIMotionBlur,**
    **    CINoiseReduction,**
    **    CIZoomBlur**
    

    我们使用最常见的高斯模糊 (Gaussian Blur) 来进行举例

    NSArray *inputKeys = filter.inputKeys;
    

    可以得到这个滤镜支持两个输入属性,分别是inputImage,inputRadius
    其中inputImage指你需要添加滤镜效果的图片,inputRadius指进行高斯模糊的程度
    设置属性的方式有两种
    一种是直接通过NSDictionary赋值

    CIImage *testCIImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"testImg.jpg"].CGImage];
        NSDictionary *filterAttDict = @{@"inputImage" : testCIImage
                                        ,@"inputRadius" : [NSNumber numberWithDouble:5.0f]};
        CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur" withInputParameters:filterAttDict];
        
    CIImage *outPutCIImage = [filter outputImage];
    
    CIContext *ciContext = [CIContext contextWithOptions:nil];
        CGImageRef cgImage = [ciContext createCGImage:outPutCIImage fromRect:outPutCIImage.extent];
        UIImage *resImage = [UIImage imageWithCGImage:cgImage];
    

    另一种是通过kvc方法赋值

        CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
        
        CIImage *testCIImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"testImg.jpg"].CGImage];
        
        [filter setValue:testCIImage forKeyPath:kCIInputImageKey];
        [filter setValue:@50 forKeyPath:kCIInputRadiusKey];
        
        CIImage *outPutCIImage = [filter outputImage];
        
        CIContext *ciContext = [CIContext contextWithOptions:nil];
        CGImageRef cgImage = [ciContext createCGImage:outPutCIImage fromRect:outPutCIImage.extent];
        UIImage *resImage = [UIImage imageWithCGImage:cgImage];
    

    发现一个写的比较详细的博客,可以参考 http://www.jianshu.com/p/d115836ed3fa

  • 相关阅读:
    SpringMVC从Request域中获取数据
    SpringMVC重定向
    SpringMVC的请求转发的三种方法
    SpringMVC文件上传
    SpringMVC处理请求释放静态资源的三种方式
    jackson实现json转换
    SpringMVC之请求部分
    SpringMVC的执行流程
    Java [Leetcode 39]Combination Sum
    深入解析Java对象的hashCode和hashCode在HashMap的底层数据结构的应用
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/11346632.html
Copyright © 2020-2023  润新知