views:

25

answers:

1

When I use the following code:

UIImage *image=[UIImage imageNamed:@"loginf1.png"];
CGImageRef rawImageRef=image.CGImage;

const float colorMasking[6] = {222, 255, 222, 255, 222, 255};

CGImageRef maskedImageRef=CGImageCreateWithMaskingColors(rawImageRef, colorMasking);

maskedImageRefis always nil. Why is this, and what can I do to correct this?

+1  A: 

I had the same problem. The CGImageRef you are creating has only 6 bytes for each pixel with byte with no alpha channel. The masking function needs a CGImageRef with 8 bytes for each pixel, only 6 of them used, with no alpha channel. At least, I think this is what's causing it.

Anyway, fix it by creating a bitmap context, drawing your image to that bitmap context, then getting your CGImageRef from CGBitmapContextCreateImage.

It's a pain in the ass and not exactly memory friendly, but it's the only way I could figure.

-Connor

Connor Lynch