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

时间:2012-02-10 09:51:26

标签: ios image-processing core-graphics

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

enter image description here

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

我想使用CoreGraphics完成所有这些。

1 个答案:

答案 0 :(得分:4)

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

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创建的上下文。

相关问题