通过Core Image添加滤镜

作者: 一双鱼jn | 来源:发表于2016-09-29 18:45 被阅读157次

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 -- 属性类型

相关文章

  • 通过Core Image添加滤镜

    by : http://cocacola-ty.github.io 处理过程 读取图片读取UIImage类型图片将...

  • iOS实现模糊效果

    Core Image Core Image 是苹果用来简化图片处理的框架,Core Image 都提供了大量的滤镜...

  • iOS-CoreImage滤镜效果预览

    闲来无事写的 Core Image 里的滤镜效果的Demo,目前 Core Image 有100多种滤镜效果,De...

  • Core Image 创建,组合滤镜

    Core Image API架构 Core Image的插件架构允许编写自定义滤镜与系统滤镜集成来扩展功能。Cor...

  • 图文并茂带你走进Core Image

    Core Image框架是适合图片的苹果滤镜框架,主要用处可以给图片添加滤镜效果和图像识别功能(人脸、条形码等等)...

  • Core Image

    Core Image前言 Core Image是iOS推出一个进行图像处理相关的库,也可以进行视频方面的处理。滤镜...

  • 使用Core Image 滤镜

    CoreImage 是一个功能强大的框架,通过这个框架很容易对图像进行处理,比如模糊,色彩,像素等,使用Core...

  • iOS图形处理篇:Core Image

    Core Image 滤镜是 iOS 5 的新增框架,通过使用这个框架,程序可以非常容易地对图片进行各种特效处理,...

  • Core Image编程指南翻译五(查询滤镜系统)

    示例代码下载 查询滤镜系统 Core Image提供的方法允许您在系统中查询可用的内置滤镜以及有关每个滤镜的显示名...

  • 1.IOS(swift)-自动增强图片效果和负片滤镜

    Core Image的自动强图片效果,会分析图像的直方图,图像属性,脸部区域,然后通过一组滤镜来改善图片效果。 自...

网友评论

    本文标题:通过Core Image添加滤镜

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