一、首先来创建一下滑动条吧!
- (void)addSlider {
for (int i = 0; i < self.chooseArray.count; i++) {
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake((kScreenWidth-200)/2, kScreenHeight-180, 200, 30)];
[slider addTarget:self action:@selector(beautifyImage:) forControlEvents:UIControlEventValueChanged];
slider.hidden = YES;
slider.tag = 51000 + i;
slider.maximumValue = 100;
if (i == 3) {
slider.minimumValue = 0;
slider.value = 0;
} else if (i == 5) {
slider.minimumValue = 0;
slider.value = 0;
} else if (i == 6) {
slider.minimumValue = 0;
slider.value = 100;
} else {
slider.minimumValue = -100;
slider.value = 0;
}
[self.view addSubview:slider];
[self.sliderArray addObject:slider];
}
}```
二、根据slider调节
-
(void)beautifyImage:(UISlider *)slider {
self.inputImage = [CIImage imageWithCGImage:self.showImage.CGImage];
CIFilter *filter = nil;NSInteger index = slider.tag - 51000;
if (index == 0 || index == 1 || index == 2) {filter = [CIFilter filterWithName:@"CIColorControls"];
} else if (index == 3) {
filter = [CIFilter filterWithName:@"CISepiaTone"];
} else if (index == 4){
filter = [CIFilter filterWithName:@"CIHueAdjust"];
} else if (index == 5) {
filter = [CIFilter filterWithName:@"CIGaussianBlur"];
} else if (index == 6) {
filter = [CIFilter filterWithName:@"CIHighlightShadowAdjust"];
}
[filter setValue:self.inputImage forKey:kCIInputImageKey];
switch (index) {
//亮度 -1---1 默认为0
case 0: {
[filter setValue:[NSNumber numberWithFloat:(slider.value/200)] forKey:@"inputBrightness"];
}
break;
//对比度 0---4 默认为1
case 1: {
CGFloat value = 0;
if (slider.value <= 0) {
value = (slider.value + 100)/200 + 0.5;
} else {
value = (slider.value + 50)/100 + 1;
}
[filter setValue:[NSNumber numberWithFloat:value] forKey:@"inputContrast"];
}
break;
//饱和度 0---2 默认为1
case 2: {
CGFloat value = 0;
if (slider.value < 0) {
value = (slider.value + 100) / 200 + 0.5;
} else if (slider.value > 0) {
value = (slider.value / 200) + 1;
}
[filter setValue:[NSNumber numberWithFloat:value] forKey:@"inputSaturation"];
}
break;
//色温 0 -- 1 默认为1
case 3: {
[filter setValue:[NSNumber numberWithFloat:slider.value/200] forKey:@"inputIntensity"];
}
break;
//色调 -3.14 -- 3.14 默认为0
case 4: {
[filter setValue:[NSNumber numberWithFloat:(slider.value * M_PI)/500] forKey:@"inputAngle"];
}
break;
//模糊 0 -- 100 默认为10
case 5: {
[filter setValue:[NSNumber numberWithFloat:slider.value/50] forKey:@"inputRadius"];
}
break;
//高亮 0.3 -- 1 默认为1
case 6: {
[filter setValue:[NSNumber numberWithFloat:(slider.value / 200 + 0.5)] forKey:@"inputHighlightAmount"];
}
break;
default:
return;
}
NSString *name = self.chooseArray[index];
self.valueLabel.text = [NSString stringWithFormat:@"%@ %1.f",name,slider.value];
// 转换图片, 创建基于GPU的CIContext对象
CIContext *context = [CIContext contextWithOptions: nil];
// 得到过滤后的图片
CIImage *outputImage = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
//显示图片的ImageView
self.showImageView.image = newImage;
// self.inputImage = outputImage;
// 释放C对象
CGImageRelease(cgimg);
}```
三、如何查看CIFilter的所有属性?
- (void)showFilertName {
NSArray *cifilter = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
NSLog(@"FilterName:\n%@,,,===%ld", cifilter,cifilter.count);//显示所有过滤器名字
for (NSString *filterName in cifilter) {
CIFilter *fltr = [CIFilter filterWithName:filterName];
//用一个过滤器名字生成一个过滤器CIFilter对象
NSLog(@":\n%@", [fltr attributes]);
}
}```