I want to use Quartz to make one color image of another color

I am sure that the answer for this exists in stackoverflow, but I searched and could not find it yet. In fact, I have several images with a transparent background and some black images, I want to be able to change the color of the black part to some other arbitrary color.

I'm sure this is possible with Quartz Image Masking , but I could not find any good examples, so this is what I am looking for. Other solutions are also welcome.

Thanks!!!

UPDATE: I think I'm very close with this code ... but my mask does not work. However, my UIView filled with fill color, but it's just a giant rectangle that doesn't get cropped.

UPDATE # 2: I'm even closer (I think) with the following code. The problem is that my background is now black, not 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); 
0
source share
1 answer

The answer is already in the question above. This was not entirely true, because I created the image in IB (in the storyboard) and did not set the background color. Somehow it was black by default. SO FRUSTRATING !!!

In any case, 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); 
+1
source

Source: https://habr.com/ru/post/1335171/


All Articles