繁体   English   中英

我如何对UIImage进行去饱和处理?

[英]How do I desaturate a UIImage?

在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

这似乎比我预期的要复杂一些。 有比这更简单的方法吗?

我在想Core Image,但我似乎无法找到一个关于如何使用它去饱和图像的具体例子。

您可以使用我的开源GPUImage框架使用两行或三行代码执行此操作:

UIImage *inputImage = [UIImage imageNamed:@"inputimage.png"];    
GPUImageGrayscaleFilter *grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];
UIImage *grayscaleImage = [grayscaleFilter imageByFilteringImage:inputImage];

(如果不使用ARC构建,请记住释放过滤器)

这将图像降低到其亮度值,使其饱和。 如果需要变量饱和度/去饱和度,可以使用GPUImageSaturationFilter。 正如框架名称所示,此过滤在GPU上运行,并且几乎在我从iOS 5.1开始基准测试的所有情况下都比Core Image快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM