标签:rcu state 分类 iat nod result geometry input 替代
kCICategoryBlur 模糊,比如高斯模糊、焦点模糊、运动模糊
按使用场景分类:
kCICategoryStillImage 用于静态图像
kCICategoryVideo 用于视频
kCICategoryInterlaced 用于交错图像
kCICategoryNonSquarePixels 用于非矩形像素
kCICategoryHighDynamicRange 用于HDR
1.实例CIImage -> 先把UIImage -> CGImageRef -> CIImage
2.创建CIFilter滤镜并给滤镜设置属性(KVC)
3.创建CIContext上下文
4.初始化一个CGImageRef 输出图片对象 合并滤镜输出的图像
5.赋给UIImage对象进行显示
6.如果想使用滤镜链 可以再次添加效果
代码示例:
#import "ViewController.h"//宏定义 屏幕的宽
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)
//注意挂上代理
@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
UIImageView *myImageView;//接收图片的视图
UIButton *photoButton;//从本地相册选择图片的按钮
UIButton *filterButton;//添加滤镜的按钮
UIButton *saveButton;//滤镜后保存到本地相册的按钮
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//去除导航栏的高度
self.edgesForExtendedLayout = UIRectEdgeNone;
//设置背景色
self.view.backgroundColor = [UIColor greenColor];
// 创建按钮
NSArray *titleButtonList = @[@"photo",@"Filter",@"save"];
for (int i=0; i<titleButtonList.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20+80*i, 20, 60, 40);
[button setTitle:titleButtonList[i] forState:UIControlStateNormal];
button.tag = 10 +i ;
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor cyanColor];
[self.view addSubview:button];
}
// 初始化图片视图
myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, SCREEN_WIDTH-40, 300)];
myImageView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:myImageView];
photoButton = [self.view viewWithTag:10];
filterButton = [self.view viewWithTag:11];
saveButton = [self.view viewWithTag:12];
// 给三个按钮添加触发事件
[photoButton addTarget:self action:@selector(photoAction:) forControlEvents:UIControlEventTouchUpInside];
// 滤镜按钮
[filterButton addTarget:self action:@selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
//保存滤镜后图片的按钮
[saveButton addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
}
//选择图片
- (void)photoAction:(UIButton *)sender{
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];
}
//把图片放在图片视图上
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = info[UIImagePickerControllerOriginalImage];
myImageView.image = image;
[self dismissViewControllerAnimated:YES completion:nil];
}
//滤镜按钮的触发方法
- (void)filterAction:(UIButton *)sender{
// 1.源图
CIImage *inputImage = [CIImage imageWithCGImage:myImageView.image.CGImage];
// 2.滤镜
CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];
// NSLog(@"%@",[CIFilter filterNamesInCategory:kCICategoryColorEffect]);//注意此处两个输出语句的重要作用
NSLog(@"%@",filter.attributes);
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[CIColor colorWithRed:1.000 green:0.165 blue:0.176 alpha:1.000] forKey:kCIInputColorKey];
CIImage *outImage = filter.outputImage;
[self addFilterLinkerWithImage:outImage];
}
//再次添加滤镜 形成滤镜链
- (void)addFilterLinkerWithImage:(CIImage *)image{
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:image forKey:kCIInputImageKey];
[filter setValue:@(0.5) forKey:kCIInputIntensityKey];
// 在这里创建上下文 把滤镜和图片进行合并
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef resultImage = [context createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
myImageView.image = [UIImage imageWithCGImage:resultImage];
}
//保存滤镜后的图片到本地相册
- (void)saveAction:(UIButton *)sender{
UIImageWriteToSavedPhotosAlbum(myImageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//保存成功调用的方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
@end
这也给了iOS新的图像滤镜,以至于我都等不及把它加到我自己的Nodality应用里面了。这同时也也意味着代码和功能可以在类和设备上分享,我已经在设想一个Nodality的桌面版本了。
那么,这些新滤镜是神马玩意? 这么说吧,CIFilter这个类里有类方法filterNamesInCategories(),用来返回一个包含所有可用滤镜的数组。下面快速浏览一下iOS8和9之间的差异吧:
CIAreaAverage - 返回一个单像素图像,其中包含一块颜色区内的平均颜色。
CIAreaMaximum - 返回一个单像素图像,其中包含一块颜色区内最大的颜色成分。
CIAreaMaximumAlpha - 返回一个单像素图像,其中包含颜色区中最大透明度的颜色矢量。
CIAreaMinimum - 返回一个单像素图像,其中包含颜色区中最小颜色成分。
CIAreaMinimumAlpha - 返回一个单像素图像,其中包含颜色区内的最小透明度的颜色矢量。
CIBoxBlur - 在一个矩形内使得图像模糊化。
CICircularWrap - 用一个透明的圆圈环绕图像。
CICMYKHalftone - 创建一个颜色,使得源图像呈半色调,在白色页面中使用使用青色,品红色,黄色和墨色。
CIColumnAverage - 返回一个高为1像素的图像,包含每个扫描列的平均颜色。
CIComicEffect - 像漫画书一样勾勒(图像)边缘,并应用半色调效果。
CIConvolution7X7 - 用一个7x7旋转矩阵来调整像素值。
CICrystallize - 通过汇集源像素的颜色值,创建多边形色块。
CIDepthOfField - 模拟一个场景深入的效果。
CIDiscBlur - 在一个圆盘形状内模糊化图像。
CIDisplacementDistortion - 将第二图像的灰度值应用到第一图像。
CIDroste - 用类似M.C.埃舍尔绘图方式递归地绘制图像的一部分。
CIEdges - 用颜色显示图像的边缘。
CIEdgeWork - 产生一个黑白风格的类似木块切口的图像。
CIGlassLozenge - 创建一个菱形滤镜,并扭曲滤镜位置的图像。
CIHeightFieldFromMask - 产生一个连续的三维物体,一个阁楼形的灰场。
CIHexagonalPixellate - 用所替换的像素映射彩色六边形的图像。
CIKaleidoscope - 从源图像中通过将12路对称,产生一个五颜六色的图象。
CILenticularHaloGenerator - 模拟闪光灯效果。
CILineOverlay - 创建草图,用黑色勾勒出图像的边缘。
CIMedianFilter - 计算一组邻近像素的平均数,然后用平均数替代每个像素的值。
CINoiseReduction - 通过降低噪声的限定值来降低噪音。
CIOpTile - 先分割图像,施加一些指定的缩放和旋转,然后拼接图像,形成的艺术化的表现。
CIPageCurlTransition - 使用翻页效果从一个图像转换到另一个图像,翻卷后显示新的图像。
CIPageCurlWithShadowTransition - 使用翻页效果从一个图像转换到另一个图像,翻卷后显示新的图像。
CIParallelogramTile - 展示一个在平行四边形内的图像。
CIPassThroughColor
CIPassThroughGeom
CIPDF417BarcodeGenerator
CIPointillize - 呈现一个pointillistic风格的源图像。
CIRippleTransition - 图像创建一个圆形波从中心点向外扩大,在波形里显示新图像。
CIRowAverage - 返回1个像素高的图像,其中包含每行扫描的平均颜色。
CIShadedMaterial - 从一个高度场产生一个阴影图像。
CISpotColor - 用色点替换颜色范围。
CISpotLight - 图像使用一个方向聚光灯效果呈现。
CIStretchCrop - 图像通过拉伸和或裁剪以适合目标尺寸。
CISunbeamsGenerator - 图像呈现阳光照射的效果。
CITorusLensDistortion - 创建环形滤镜,并扭曲透镜位置的图像。
CITriangleTile - 截取图像的一个三角形部分映射到一个三角形区域,然后平铺展示。
标签:rcu state 分类 iat nod result geometry input 替代
原文地址:http://www.cnblogs.com/diweinan/p/6257576.html