通过Core Image添加滤镜

by : http://cocacola-ty.github.io

处理过程

  1. 读取图片

    1. 读取UIImage类型图片

    2. 将UIImage转为NSData

    3. 根据NSData转为CIImage

  2. 处理滤镜

    1. 创建滤镜。根据滤镜名称初始化一个滤镜

    2. 设置滤镜参数

    3. 设置滤镜图片输入源,即设置滤镜的inputImage参数

  3. 处理进行过滤镜的图片

    1. 获取CIContext,通过该对象将滤镜的输出图像进行处理

    2. 通过CIContext对象获取滤镜的输出对象并将图像转换为CGImage

    3. 将CGImage转换为UIImage

主要类

  • CIContext: Core Image处理上下文,图像处理在该类完成

  • CIFilter:滤镜类,定义各种滤镜的属性。各种滤镜都属于该类

  • CIImage:保存图像数据

实例

        /******读取图片部分******/
    // 读取图片
    NSString *path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    
    // 将UIImage转CIImage
    NSData *data = UIImagePNGRepresentation(image);
    CIImage *inputImage = [CIImage imageWithData:data];
    

    /******处理滤镜部分******/
    // 初始化进行滤镜的颜色
    CIColor *sepiaColor = [CIColor colorWithRed:0.76 green:0.65 blue:0.54];
    
    // 通过滤镜名称创建滤镜 初始化滤镜参数
    CIFilter *monochromeFilter = [CIFilter filterWithName:@"CIColorMonochrome" withInputParameters:@{@"inputColor":sepiaColor,@"inputIntensity":@1.0}];
    // 设置滤镜的输入图像
    [monochromeFilter setValue:inputImage forKey:@"inputImage"];
    
    // 根据滤镜名称创建滤镜 初始化滤镜基本参数
    CIFilter *vinFilter = [CIFilter filterWithName:@"CIVignette" withInputParameters:@{@"inputRadius":@1.75,@"inputIntensity":@1.0}];
    // 以经过上一个滤镜处理的图像作为本次滤镜的输入图像
    [vinFilter setValue:monochromeFilter.outputImage forKey:@"inputImage"];
    
    // 获取经过两次滤镜的图像
    CIImage *outputImage = vinFilter.outputImage;
    
    // 创建高斯模糊滤镜
    CIFilter *blur = [CIFilter filterWithName:@"CIGaussianBlur" withInputParameters:@{@"inputRadius":@40}];
    [blur setValue:inputImage forKey:@"inputImage"];
    

    /******处理滤镜之后图片部分******/
    // 将CIImage转换为CGImage 再转为UIImage
    CIContext *context = [CIContext contextWithOptions:nil];
    struct CGImage *cgImage = [context createCGImage:blur.outputImage fromRect:inputImage.extent];
    
    UIImage *outputUIImage = [UIImage imageWithCGImage:cgImage];
    
    // 显示处理后的图片
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.frame = CGRectMake(40, 40, 200, 200);
    imgView.image = outputUIImage ;
    [self.view addSubview:imgView];

获取可用的滤镜列表

  // 所有可用的滤镜
    NSLog(@"可用的滤镜\n%@",[CIFilter filterNamesInCategory:kCICategoryBuiltIn])

获取滤镜的属性

    // 滤镜的属性
    NSLog(@"%@",blurFilter.attributes)

// 输出结果为:

{
    "CIAttributeFilterAvailable_Mac" = "10.4";
    "CIAttributeFilterAvailable_iOS" = 6;
    CIAttributeFilterCategories =     (
        CICategoryBlur,
        CICategoryStillImage,
        CICategoryVideo,
        CICategoryBuiltIn
    );
    CIAttributeFilterDisplayName = "Gaussian Blur";
    CIAttributeFilterName = CIGaussianBlur;
    CIAttributeReferenceDocumentation = "http://developer.apple.com/cgi-bin/apple_ref.cgi?apple_ref=//apple_ref/doc/filter/ci/CIGaussianBlur";
    inputImage =     {
        CIAttributeClass = CIImage;
        CIAttributeDescription = "The image to use as an input image. For filters that also use a background image, this is the foreground image.";
        CIAttributeDisplayName = Image;
        CIAttributeType = CIAttributeTypeImage;
    };
    inputRadius =     {
        CIAttributeClass = NSNumber;
        CIAttributeDefault = 10;
        CIAttributeDescription = "The radius determines how many pixels are used to create the blur. The larger the radius, the blurrier the result.";
        CIAttributeDisplayName = Radius;
        CIAttributeIdentity = 0;
        CIAttributeMin = 0;
        CIAttributeSliderMax = 100;
        CIAttributeSliderMin = 0;
        CIAttributeType = CIAttributeTypeScalar;
    };
}

inputXX即该滤镜的输入参数。(当前滤镜中即inputRadius)

这样,通过查询滤镜的attributes的inputXX数组我们可以得到滤镜的输入参数来设置滤镜的显示效果

详细解释输入参数数组

  • CIAttributeClass -- 该属性接收的类型

  • CIAttributeDefault -- 该属性的默认值

  • CIAttributeDescription -- 该属性的描述

  • CIAttributeDisplayName -- 该属性的名称

  • CIAttributeIdentity -- 属性标识

  • CIAttributeMin -- 属性最小值

  • CIAttributeSliderMax -- 属性最大值

  • CIAttributeType -- 属性类型

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 许多UIView的子类,如一个UIButton或一个UILabel,它们知道怎么绘制自己。迟早,你也将想要做一些自...
    shenzhenboy阅读 5,637评论 2 8
  • 原链接:http://www.csdn.net/article/2015-02-13/2823961-core-i...
    hament阅读 4,615评论 0 1
  • Core Image是一个强大的框架,它能够让你轻松地对图像进行过滤。你能够通过修改图像的饱和度、色调或曝光率来获...
    木易林1阅读 4,845评论 0 1
  • 前言 最近在研究 Core Image 自定义 Filter 相关内容,重新学习了 Core Image,对 Co...
    泥孩儿0107阅读 4,208评论 0 4
  • 生命在繁衍的时候既不希望下一代与其一模一样,也不希望与其完全不一样,而是要基于更强地适应环境的能力。如果环境适宜,...
    光华同学阅读 4,386评论 0 51

友情链接更多精彩内容