iOS 5.x中是否有一种简单的方法(或内置库)来降低UIImage的饱和度?这是我目前的做法:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); // flip image right side up
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, self.image.CGImage);
CGContextSetBlendMode(context, kCGBlendModeSaturation);
CGContextClipToMask(context, self.bounds, image.CGImage); // restricts drawing to within alpha channel
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, desaturation);
CGContextFillRect(context, rect);
CGContextRestoreGState(context); // restore state to reset blend mode
这似乎比我预期的要复杂一些。有没有比这更简单的方法?
我在想核心图像,但我似乎找不到如何使用它降低图像饱和度的具体示例。
您可以使用两行或三行代码使用我的开源 GPU image 框架来执行此操作:
UIImage *inputImage = [UIImage imageNamed:@"inputimage.png"];
GPUImageGrayscaleFilter *grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];
UIImage *grayscaleImage = [grayscaleFilter imageByFilteringImage:inputImage];
(如果不使用 ARC 构建,请记住释放过滤器)
这会将图像降低到其亮度值,使其饱和度降低。如果你想要可变饱和度/去饱和度,你可以使用 GPUImageSaturationFilter 。正如框架名称所示,此过滤在 GPU 上运行,并且在我从 5.1 开始在 iOS 上基准测试的几乎所有情况下都比 Core Image 更快。