iOS 修改图片的亮度、对比度、饱和度

来源:互联网 发布:base笔记软件 编辑:程序博客网 时间:2024/05/29 13:15

iOS 修改图片的亮度、对比度、饱和度

在iOS开发过程中,需要对图片的亮度、对比度、饱和度进行修改时,可以使用图像处理框架CoreImage。
注:使用CoreImage框架首先要在工程中添加“CoreImage.framework”

以下为具体代码

UIImage *myImage = [UIImage imageNamed:@"Superman"];CIContext *context = [CIContext contextWithOptions:nil];CIImage *superImage = [CIImage imageWithCGImage:myImage.CGImage];CIFilter *lighten = [CIFilter filterWithName:@"CIColorControls"];[lighten setValue:superImage forKey:kCIInputImageKey];// 修改亮度   -1---1   数越大越亮[lighten setValue:@(0.2) forKey:@"inputBrightness"];// 修改饱和度  0---2[lighten setValue:@(0.5) forKey:@"inputSaturation"];// 修改对比度  0---4[lighten setValue:@(2.5) forKey:@"inputContrast"];CIImage *result = [lighten valueForKey:kCIOutputImageKey];CGImageRef cgImage = [context createCGImage:result fromRect:[superImage extent]];// 得到修改后的图片myImage = [UIImage imageWithCGImage:cgImage];// 释放对象CGImageRelease(cgImage);
0 0