iOS图片置灰
2020-08-19 本文已影响0人
CrazySnow
通过对图片属性的操作,得到灰色图片赋值
//图片置灰操作
+(UIImage *)changeGrayImage:(UIImage *)oldImage{
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *superImage = [CIImage imageWithCGImage:oldImage.CGImage];
CIFilter *lighten = [CIFilter filterWithName:@"CIColorControls"];
[lighten setValue:superImage forKey:kCIInputImageKey];
// 修改亮度 -1---1 数越大越亮
[lighten setValue:@(0) forKey:@"inputBrightness"];
// 修改饱和度 0---2
[lighten setValue:@(0) forKey:@"inputSaturation"];
// 修改对比度 0---4
[lighten setValue:@(0.5) forKey:@"inputContrast"];
CIImage *result = [lighten valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[superImage extent]];
// 得到修改后的图片
UIImage *newImage = [UIImage imageWithCGImage:cgImage];
// 释放对象
CGImageRelease(cgImage);
return newImage;
}