Core Graphics: creating an image mask from a PDF?


Graham Cox
 

Hi all,

I am using Core Graphics. I have an image as a PDF resource that I want to use as a mask - specifically, the PDF only contains black and white graphics as vectors. I have code that can turn that into a CGImageRef as a normal bitmap (to any desired size I want, which is why I start with a PDF file), but the bitmap image I end up with is, of course, black and white. I actually want to draw that in any colour I choose by using a mask image and a fill colour.

Core Graphics has a function CGImageMaskCreate(), which takes a bunch of the usual parameters but only works with a data provider. There is no PDF data provider (AFAIK).

The bitmap image code works by creating a CGBitmapImageContext, drawing the PDF into it, then using CGBitmapContextImageCreate() to obtain the bitmap. But there’s no mask equivalent of this approach. There is also no function to create a mask image from a bitmap image.

Can anyone think of a solution here? Either to create a mask image from a PDF, or a way to draw a black and white bitmap colourised at will. The latter may be possible using CGImageCreateWithMaskingColors(), but the documentation isn’t very clear and the Quartz drawing guide seems to suggest it’s more for background removal than what I need here. If anyone has an example of how to use it (if it’s even appropriate here) that would be great too.


—Graham

Join cocoa@apple-dev.groups.io to automatically receive all group messages.