问题
I have a picture in shades of gray. Is it possible to recolor the image in shades of other (arbitrary) color? Something like on the picture below.

I think I can do that by accessing each pixel of the image and changing it as necessary, but I think that there might be a better way.
I would like to do all this using CoreGraphics.
回答1:
I solved the task using following code:
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);
In this code bitmapContext
is context created with CGBitmapContextCreate
.
来源:https://stackoverflow.com/questions/9225834/how-to-recolor-an-image-using-coregraphics