对目标图片添加滤镜
使用CoreImage框架为图片添加滤镜
创建UIImage的category,这样想要添加滤镜的图片直接调用下面的方法就可以了。
- (UIImage *)addFilterWithName:(NSString *)filterName {
UIImageView *theImageViewBack = [[UIImageView alloc]init];
theImageViewBack.image = self;
CIImage * inputImage = [CIImage imageWithCGImage:theImageViewBack.image.CGImage];
//2.创建CIFilter对象
CIFilter * filter = [CIFilter filterWithName:filterName];
//3.设置默认参数
[filter setDefaults];
//4.设置要处理的图像
[filter setValue:inputImage forKey:@"inputImage"];
//5.得到处理后的图像
CIImage * outImage = filter.outputImage;
[filter setValue:outImage forKey:kCIInputImageKey];
// 在这里创建上下文 把滤镜和图片进行合并
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef resultImage = [context createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
//这里返回的图像已经是添加了滤镜效果的图像了
return [UIImage imageWithCGImage:resultImage];
}
常用滤镜
@"CIPhotoEffectNoir",
@"CIPhotoEffectInstant",
@"CIPhotoEffectProcess",
@"CIPhotoEffectFade",
@"CIPhotoEffectTonal",
@"CIPhotoEffectMono",
@"CIPhotoEffectChrome",
@"CISepiaTone",
@"CIMedianFilter",
@"CINoiseReduction",
@"CIColorMonochrome",
@"CIColorPosterize",
@"CIFalseColor",
@"CIMaximumComponent",
@"CIMinimumComponent",
@"CIPhotoEffectChrome"