项目中要实现高斯模糊的效果,今天看了下Core Image这块的内容, 主要包括CIImage、CIFilter、CIContext、CIDetector(检测)、CIFeature(特征)等类。

今天先记录下CIImage、CIFilter、CIContext三个类的使用。

一、基本的滤镜效果需要以下步骤

1.创建CIImage对象

2.创建CIContext对象用作画布

3.创建CIFilter对象

4.输出滤镜

二、创建上面三个对象的API

1.创建CIImage对象 主要通过以下方法( 方法有好多种  具体查看类CIImage)

+ (CIImage *)imageWithCGImage:(CGImageRef)image;
+ (CIImage *)imageWithCGLayer:(CGLayerRef)layer;
+ (nullable CIImage *)imageWithContentsOfURL:(NSURL *)url;
+ (nullable CIImage *)imageWithData:(NSData *)data;
- (instancetype)initWithCGImage:(CGImageRef)image;
- (nullable instancetype)initWithData:(NSData *)data;
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url;
- (instancetype)initWithColor:(CIColor *)color;

2.创建CIContext对象

CIContext 构造函数contextWithOptions:的输入是一个NSDictionary。 它规定了各种选项,包括颜色格式以及内容是否应该运行在CPU或是GPU上。

 CIContext *context=[CIContext contextWithOptions:nil];

在该类中还有一些其他方法

- (void)drawImage:(CIImage *)image
          atPoint:(CGPoint)atPoint
         fromRect:(CGRect)fromRect NS_DEPRECATED(10_4,10_8, 5_0,6_0);

/* Render the rectangle 'fromRect' of 'image' to the rectangle 'inRect' in the
 * context's destination. */
- (void)drawImage:(CIImage *)image
           inRect:(CGRect)inRect
         fromRect:(CGRect)fromRect;

/* Render the region 'fromRect' of image 'image' into a temporary buffer using
 * the context, then create and return a new CoreGraphics image with
 * the results. The caller is responsible for releasing the returned
 * image. */
- (CGImageRef)createCGImage:(CIImage *)image
                   fromRect:(CGRect)fromRect
CF_RETURNS_RETAINED;

/* Create a new CGImage from the specified subrect of the image. If
 * non-nil the new image will be created in the specified format and
 * colorspace. */
- (CGImageRef)createCGImage:(CIImage *)image
                   fromRect:(CGRect)fromRect
                     format:(CIFormat)format
                 colorSpace:(nullable CGColorSpaceRef)colorSpace

3.创建CIFilter对象

1、创建滤镜对象

+ (nullable CIFilter *) filterWithName:(NSString *) name;

/** Creates a new filter of type 'name'.
 The filter's input parameters are set from the list of key-value pairs which must be nil-terminated.
 On OSX, any of the filter input parameters not specified in the list will be undefined.
 On iOS, any of the filter input parameters not specified in the list will be set to default values. */
+ (nullable CIFilter *)filterWithName:(NSString *)name
                        keysAndValues:key0, ... NS_REQUIRES_NIL_TERMINATION NS_SWIFT_UNAVAILABLE("");

/** Creates a new filter of type 'name'.
 The filter's input parameters are set from the dictionary of key-value pairs.
 On OSX, any of the filter input parameters not specified in the dictionary will be undefined.
 On iOS, any of the filter input parameters not specified in the dictionary will be set to default values. */
+ (nullable CIFilter *)filterWithName:(NSString *)name
                  withInputParameters:(nullable CI_DICTIONARY(NSString*,id) *)params NS_AVAILABLE(10_10, 8_0);

2、上面创建滤镜对象时需要filterName,那怎么查看name以及每个CIFilter对象的属性呢?

CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    NSLog(@"%@",filter.attributes);

在上面的代码中输出以下代码

-- :::] {
    "CIAttributeFilterAvailable_Mac" = "10.4";
    ;
    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 = ;
        CIAttributeDescription = "The radius determines how many pixels are used to create the blur. The larger the radius, the blurrier the result.";
        CIAttributeDisplayName = Radius;
        CIAttributeIdentity = ;
        CIAttributeMin = ;
        CIAttributeSliderMax = ;
        CIAttributeSliderMin = ;
        CIAttributeType = CIAttributeTypeScalar;
    };
}

从上面的输出结果我们可以看到filter有CIAttributeFilterCategories、CIAttributeFilterDisplayName、CIAttributeFilterName、inputImage、inputRadius等属性。

在CIAttributeFilterCategories中可以看到滤镜有CICategoryBlur, CICategoryStillImage,CICategoryVideo, CICategoryBuiltIn种,在CIFilter类中有下面的两个方法能遍历出所有的滤镜名

/** Returns an array containing all published filter names in a category. */
+ (CI_ARRAY(NSString*) *)filterNamesInCategory:(nullable NSString *)category;

/** Returns an array containing all published filter names that belong to all listed categories. */
+ (CI_ARRAY(NSString*) *)filterNamesInCategories:(nullable CI_ARRAY(NSString*) *)categories;

获得到滤镜名之后就可以通过attributes属性查看filter对象的属性 ,通过KVC来设置属性值

 //和UIButton类似 通过简单参数确定CIFilter子类  简单工厂
    CIFilter *filter = [CIFilter filterWithName:@"CIAffineTransform"];
    //  NSLog(@"%@",filter.attributes);
    //将图片输入到滤镜中,打个比方:滤镜是个具有某个功能的容器,任何东西放进去,拿出来的时候就会附上效果。 KVC
    [filter setValue:ciimage forKey:@"inputImage"];
    [filter setValue: [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation()] forKey:@"inputTransform"];

4.输出滤镜

1、通过UIImage的imageWithCIImage方法

//从滤镜容器中取出图片  这里还有一种输出方式:使用CIcontext  不知道这两种有什么优缺点,欢迎留言
    //CIImage *new = [filter valueForKey:kCIOutputImageKey];
    CIImage *outputImage = filter.outputImage;

    UIImageView *img = [[UIImageView alloc] initWithFrame:self.view.frame];
    img.image = [UIImage imageWithCIImage:outputImage];
    //img.image=[UIImage imageWithCIImage:outputImage scale:30 orientation:UIImageOrientationDown];
    [self.view addSubview:img];

2、通过通过UIImage的imageWithCGImage方法 此方法要用到CIContext

 //CIImage *new = [filter valueForKey:kCIOutputImageKey];
    CIImage *outputImage = filter.outputImage;

    CIContext *context=[CIContext contextWithOptions:nil];
    CGImageRef cgimg =[context createCGImage:outputImage fromRect:[outputImage extent]];

    UIImageView *img = [[UIImageView alloc] initWithFrame:self.view.frame];
    img.image=[UIImage imageWithCGImage:cgimg];
    [self.view addSubview:img];
    CGImageRelease(cgimg);

上面两种方法中,第一个每次调用都会生成一个CIContext。CIContext本来是可以重用以便提高性能和效率的。

5.完整代码

//
//  ViewController.m
//  CoreImage
//
//  Created by City--Online on 15/11/10.
//  Copyright © 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"
#import <CoreImage/CoreImage.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CIImage *inputImg = [[CIImage alloc]initWithCGImage:[UIImage imageNamed:@"1.jpg"].CGImage];

    //和UIButton类似 通过简单参数确定CIFilter子类  简单工厂
    CIFilter *filter = [CIFilter filterWithName:@"CIAffineTransform"];

    //输出属性
    NSLog(@"%@",filter.attributes);

    //将图片输入到滤镜中,打个比方:滤镜是个具有某个功能的容器,任何东西放进去,拿出来的时候就会附上效果。 KVC
    [filter setValue:inputImg forKey:@"inputImage"];
    [filter setValue: [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation()] forKey:@"inputTransform"];

    //从滤镜容器中取出图片  这里还有一种输出方式:使用CIcontext  不知道这两种有什么优缺点,欢迎留言
    //CIImage *new = [filter valueForKey:kCIOutputImageKey];
    CIImage *outputImage = filter.outputImage;

    //通过CIContext上下文 、imageWithCGImage输出滤镜
    CIContext *context=[CIContext contextWithOptions:nil];
    CGImageRef cgimg =[context createCGImage:outputImage fromRect:[outputImage extent]];

    UIImageView *img = [[UIImageView alloc] initWithFrame:self.view.frame];
    img.image=[UIImage imageWithCGImage:cgimg];
    [self.view addSubview:img];
    CGImageRelease(cgimg);

    //遍历每种滤镜下的滤镜名
    NSLog(@"%@",[CIFilter filterNamesInCategories:@[@"CICategoryBlur"]]);
    NSLog(@"%@",[CIFilter filterNamesInCategories:@[@"CICategoryVideo"]]);
    NSLog(@"%@",[CIFilter filterNamesInCategories:@[@"CICategoryStillImage"]]);
    NSLog(@"%@",[CIFilter filterNamesInCategories:@[@"CICategoryBuiltIn"]]);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

6.效果

IOS Core Image之一的更多相关文章

  1. 转 iOS Core Animation 动画 入门学习(一)基础

    iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...

  2. iOS - Core Animation 核心动画

    1.UIView 动画 具体讲解见 iOS - UIView 动画 2.UIImageView 动画 具体讲解见 iOS - UIImageView 动画 3.CADisplayLink 定时器 具体 ...

  3. IOS Core Image之二

    在上篇博客IOS Core Image之一中了解了下CIImage.CIFilter.CIContext三个类的使用,这篇了解下滤镜链(多滤镜)和人脸检测(不是人脸识别). 一.多滤镜 1.有些效果不 ...

  4. iOS Core ML与Vision初识

    代码地址如下:http://www.demodashi.com/demo/11715.html 教之道 贵以专 昔孟母 择邻处 子不学 断机杼 随着苹果新品iPhone x的发布,正式版iOS 11也 ...

  5. iOS Core Animation 简明系列教程

    iOS Core Animation 简明系列教程  看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽 ...

  6. IOS Core Animation Advanced Techniques的学习笔记(五)

    第六章:Specialized Layers   类别 用途 CAEmitterLayer 用于实现基于Core Animation粒子发射系统.发射器层对象控制粒子的生成和起源 CAGradient ...

  7. IOS Core Animation Advanced Techniques的学习笔记(四)

    第五章:Transforms   Affine Transforms   CGAffineTransform是二维的     Creating a CGAffineTransform   主要有三种变 ...

  8. IOS Core Animation Advanced Techniques的学习笔记(一)

    转载. Book Description Publication Date: August 12, 2013 Core Animation is the technology underlying A ...

  9. iOS Core Animation之CALayer心得

    使用CALayer的mask实现注水动画效果 Core Animation一直是iOS比较有意思的一个主题,使用Core Animation可以实现非常平滑的炫酷动画.Core animtion的AP ...

  10. iOS——Core Animation 知识摘抄(四)

    原文地址http://www.cocoachina.com/ios/20150106/10840.html 延迟解压 一旦图片文件被加载就必须要进行解码,解码过程是一个相当复杂的任务,需要消耗非常长的 ...

随机推荐

  1. CSS content应用

    一.简介 content属性早在 CSS2.1的时候就被引入了,可以使用:before以及:after伪元素生成内容.此特性目前已被大部分的浏览器支持:(Firefox 1.5+, Safari 3. ...

  2. File Path Directory总结

    阅读目录 开始 Path 对路径 字符串进行操作 获得后缀 能合并路径 获取文件名 Directory和DirectoryInfo  对目录进行操作 判断目录是否存在 创建目录 删除目录 获取目录下所 ...

  3. mac下wordpress环境搭建

    mac下本来就有apache和php,只需要配置以下+安装mysql 1.Apache 预装目录在  /etc/apache2: 默认的网站目录在 /Library/WebServer/Documen ...

  4. “全栈2019”Java异常第十八章:Exception详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  5. “全栈2019”Java第八十八章:接口中嵌套接口的应用场景

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. 【文文殿下】Manache算法-学习笔记

    Manache算法 定义:是一个判断回文子串的算法,我们结合例题解释: 题目:给定一个长度为 n 的字符串 S,求其最长回文子串 一个字符串是回文的,当且仅当反转后的串与原串完全相等 分析:对于这个题 ...

  7. MySQL之LIMIT用法

    http://blog.163.com/niuxiangshan@126/blog/static/17059659520101081058299/ 看的人家的 mysql支持limitselect * ...

  8. mysql分表分库选型

    三个方向: 一.应用中自己实现,可直连数据库,自己实现因目前使用的Mybatis框架可以使用两种做法: 1.在SQL中直接分表逻辑,我查了一下代码中的sql,发现join的千姿百态,不大动这些sql这 ...

  9. kali系统越来越大解决

    Kali Linux系统提供的apt-get方式,可以很好的安装软件,对系统进行更新.但是每次执行都会下载大量的软件包.这些软件包被安装后,并不会被自动删掉,会持续占用磁盘空间.解决这个问题有两个办法 ...

  10. LLDB 和Chisel 使用例子

    打印变量 打印数字 (lldb) p/d 16 16 16 进制格式 (lldb) p/x 16 0x10 2 进制格式 (lldb) p/t 16 0b00000000000000000000000 ...