CoreImage 图片处理
CoreImage 是苹果 iOS5新增的一个 OC 框架,提供了强大的图像处理功能, 用于对基于像素的图像进行操作与分析, 提供了很多滤镜(Filter),形成强大的自定义效果
CIImage 类
用于保存图像数据, 初始化方法:
1.imageWithCGImage:
2.imageWithContentsOfURL:
3.imageWithCVImageBuffer:
4.imageWithData:
CFFilter 滤镜类
实例化滤镜对象,通过键-值KVC来设置输入值,及效果的改变.
1. 通常 CIFilter 对象需要一个或多个图像作为输入,-> 2.通过 KVC 设置一些参数 -> 3.并产生 CIImage 类型的实体作为输出
CIFilter * filter = [CIFilter filterWithName:@"CIGaussianBlur"];
下面是查询需要的滤镜种类的方法:
1.查询 效果分类中 包含什么效果:filterNamesInCategory:
(1)按住command 点击CIFilter 进入接口文件 找到第128行-148行全部都是 效果分类
(2)选择其中某一个分类 NSLog -> [CIFilter filterNamesInCategory:刚才拷贝的分类]; -> 打印出来的 是这个分类包含的所有 效果 -> 拷贝选择其中的某一个效果.
2.查询 使用的效果中 可以设置什么属性(KVC) attributes
NSLog -> [CIFilter filterWithName:刚才拷贝选择其中的某一个效果].attributes ->得到这个滤镜所有可以设置的属性
调用[CIFilter attributes]会返回filter详细信息,
CIContext 上下文
CIContext 又称上下文用来渲染 CIImage, 将作用在 CIImage 上的滤镜链 应用到原始的图片数据
得到处理后的图片:
使用步骤:
1.创建 CIImage对象
2.创建 CIFilter 滤镜对象
3.利用 KVC 设置滤镜对象的属性
4.通过滤镜对象获取处理后图片
5.创建 CIContext对象, 将得到的图片渲染到视图上
🌰例子:
//模糊设置处理
-(void)filterGaussianBlur { //创建CIContext对象 CIContext * context = [CIContext contextWithOptions:nil]; //获取图片 CIImage * image = [CIImage imageWithCGImage:[_image CGImage]]; //创建CIFilter CIFilter * gaussianBlur = [CIFilter filterWithName:@"CIGaussianBlur"]; //设置滤镜输入参数 [gaussianBlur setValue:image forKey:@"inputImage"]; //设置模糊参数 [gaussianBlur setValue:[NSNumber numberWithFloat:_slider.value*10] forKey:@"inputRadius"]; //得到处理后的图片 CIImage* resultImage = [gaussianBlur valueForKey:@"outputImage"]; CGImageRef imageRef = [context createCGImage:resultImage fromRect:CGRectMake(0,0,self.image.size.width,self.image.size.height)]; UIImage * image1 = [UIImage imageWithCGImage:imageRef]; [_imageView setImage:image1]; CFRelease(imageRef); }
//旧色调处理
-(void)filterSepiaTone { //创建CIContext对象(默认值,传入nil) CIContext * context = [CIContext contextWithOptions:nil]; //获取图片 CIImage *cimage = [CIImage imageWithCGImage:[_image CGImage]]; //创建CIFilter CIFilter * sepiaTone = [CIFilter filterWithName:@"CISepiaTone"]; //设置滤镜输入参数 [sepiaTone setValue:cimage forKey:@"inputImage"]; //获取滑块的Value,设置色调强度 [sepiaTone setValue:[NSNumber numberWithFloat:[_slider value]]forKey:@"inputIntensity"]; //创建处理后的图片 CIImage * resultImage = [sepiaTone valueForKey:@"outputImage"]; CGImageRef imageRef = [context createCGImage:resultImage fromRect:CGRectMake(0,0,self.image.size.width,self.image.size.height)]; UIImage * image = [UIImage imageWithCGImage:imageRef]; [_imageView setImage:image]; CFRelease(imageRef); }
参考:
https://www.jianshu.com/p/cd7bb24a68d0
https://www.jianshu.com/p/5ac1b631c33e
//获取 fileterName
https://www.jianshu.com/p/31dc42d691ac