CIFilter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
+(void) adjustImage:(UIImage *) image container:(UIImageView *) iv{
CIImage *beginImage = [CIImage imageWithCGImage:image.CGImage];
CIFilter * filter = [CIFilter filterWithName:@"CIColorControls"];
[filter setValue:beginImage forKey:kCIInputImageKey];

NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

// 亮度 10 -1---1
float lightPercent = [[ud objectForKey:@"Light"] floatValue];
float lightValue = -1 + 2*lightPercent;


// 对比度 -11 0---4
float contrastPercent = [[ud objectForKey:@"Contrast"] floatValue];
float contrastValue = 0 + 4*contrastPercent;

// 饱和度 0---2
float bhdPercent = [[ud objectForKey:@"Temp"] floatValue];
float bhdValue = 0 + 2*bhdPercent;

[filter setValue:@(lightValue) forKey:@"inputBrightness"];
[filter setValue:@(contrastValue)forKey:@"inputContrast"];
[filter setValue:@(bhdValue) forKey:@"inputSaturation"];

// 得到过滤后的图片
CIImage *outputImage = [filter outputImage];
// // 转换图片, 创建基于GPU的CIContext对象
// CIContext *context = [CIContext contextWithOptions: nil];
// CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCIImage:outputImage];
// // 显示图片
[iv setImage:newImg];
// // 释放C对象
// CGImageRelease(cgimg);

}