如何使用CoreGraphics重新着色图像?

我有一张灰色的照片。 是否可以以其他(任意)颜色的色调重新着色图像? 如下图所示。

在此处输入图像描述

我想我可以通过访问图像的每个像素并根据需要更改它来做到这一点,但我认为可能有更好的方法。

我想用CoreGraphics做这一切。

我使用以下代码解决了任务:

CGContextSaveGState(bitmapContext); CGContextSetRGBFillColor(bitmapContext, red, green, blue, alpha); CGContextTranslateCTM(bitmapContext, 0.0, contextHeight); CGContextScaleCTM(bitmapContext, 1.0, -1.0); CGContextDrawImage(bitmapContext, sourceImageRect, sourceImage); CGContextSetBlendMode(bitmapContext, kCGBlendModeColor); CGContextFillRect(bitmapContext, sourceImageRect); CGContextRestoreGState(bitmapContext); CGImageRef coloredImage = CGBitmapContextCreateImage(bitmapContext); 

在此代码中, bitmapContext是使用CGBitmapContextCreate创建的上下文。