我想使用Quartz将单色图像制作为其他颜色 [英] I want to use Quartz to make a single color image a different color

查看:105
本文介绍了我想使用Quartz将单色图像制作为其他颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信在stackoverflow上已经找到了答案,但是我一直在寻找并且还没有找到它。本质上,我有几张具有透明背景的图像和一些黑色图像,我希望能够将黑色部分的颜色更改为其他任意颜色。

I'm sure the answer for this exists on stackoverflow, but I've been looking and haven't been able to find it yet. Essentially, I have several images with transparent backgrounds and some black imagery, I want to be able to change the color of the black part to some other arbitrary color.

我可以相对确定地使用 Quartz Image Masking ,但是我一直找不到任何好的例子,所以这就是我想要的。也欢迎使用其他解决方案。

I'm relatively sure this is achievable with Quartz Image Masking, but I haven't been able to find any good examples, so that is what I'm looking for. Other solutions are also welcome.

谢谢!

更新:我认为我与这段代码非常接近...但是我的面具不起作用。我的 UIView 确实被填充颜色填充了,但它只是一个巨大的矩形,没有被剪切。

UPDATE: I think I'm pretty close with this code... but my mask isn't working. My UIView does get filled with the fill color though, but it's just a giant rectangle, not clipped whatsoever.

更新#2:我(现在)与以下代码更加接近。问题是我的背景现在是黑色,而不是透明的。

UPDATE #2: I am even closer now (I think) with the following code. The problem is that my background is now black, rather than transparent.

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);

CGImageRef maskImage = [self.image CGImage];
CGContextClipToMask(context, rect, maskImage);

[_colorToChangeInto setFill];
CGContextFillRect(context, rect);


推荐答案

答案已经在上面的问题中。这不是很有效,因为我是在IB(在情节提要中)中创建图像的,并且没有设置背景色。不知何故默认为Black。如此烦躁!!!

The answer is already in the question above. It wasn't quite working because I was creating the image in IB (in a storyboard), and hadn't set the background color. Somehow that defaulted to Black. SO FRUSTRATING!!!

无论如何,在我将背景色更改为透明后,以下代码就像一个吊饰一样!

Anyway, after I changed the background color to transparent, the following code works like a charm!

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);

CGImageRef maskImage = [self.image CGImage];
CGContextClipToMask(context, rect, maskImage);

[_colorToChangeInto setFill];
CGContextFillRect(context, rect);

这篇关于我想使用Quartz将单色图像制作为其他颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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