如何使用CoreGraphics重新着色图像? [英] How to recolor an image using CoreGraphics?

查看:165
本文介绍了如何使用CoreGraphics重新着色图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想用CoreGraphics完成所有这些。

I would like to do all this using CoreGraphics.

推荐答案

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

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);

在此代码中 bitmapContext 是使用 CGBitmapContextCreate

这篇关于如何使用CoreGraphics重新着色图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆