How can I change the saturation of an UIImage?

后端 未结 6 1594
旧巷少年郎
旧巷少年郎 2021-01-30 05:42

I have an UIImage and want to shift it\'s saturation about +10%. Are there standard methods or functions that can be used for this?

6条回答
  •  無奈伤痛
    2021-01-30 06:25

    There's a CoreImage filter for this. CIColorControls Just set the inputSaturation to < 1.0 to desaturate or > 1.0 to increase saturation...

    eg. Here's a method I've added in a category on UIImage to desaturate an image.

    -(UIImage*) imageDesaturated {
        CIContext *context = [CIContext contextWithOptions:nil];
        CIImage *ciimage = [CIImage imageWithCGImage:self.CGImage];
        CIFilter *filter = [CIFilter filterWithName:@"CIColorControls"];
        [filter setValue:ciimage forKey:kCIInputImageKey];
        [filter setValue:@0.0f forKey:kCIInputSaturationKey];
        CIImage *result = [filter valueForKey:kCIOutputImageKey];
        CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
        UIImage *image = [UIImage imageWithCGImage:cgImage];
        CGImageRelease(cgImage);
        return image;
    }
    

提交回复
热议问题