by : http://cocacola-ty.github.io
处理过程
-
读取图片
-
读取UIImage类型图片
-
将UIImage转为NSData
-
根据NSData转为CIImage
-
-
处理滤镜
-
创建滤镜。根据滤镜名称初始化一个滤镜
-
设置滤镜参数
-
设置滤镜图片输入源,即设置滤镜的inputImage参数
-
-
处理进行过滤镜的图片
-
获取CIContext,通过该对象将滤镜的输出图像进行处理
-
通过CIContext对象获取滤镜的输出对象并将图像转换为CGImage
-
将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 -- 属性类型
网友评论