How to recolor an image using CoreGraphics?

会有一股神秘感。 提交于 2019-12-24 02:27:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!