views:

48

answers:

1

I've masked out my image thusly:

    CGImageRef maskRef = [[UIImage imageNamed:@"testMask2.png"] CGImage];

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), nil, YES);

UIImage *image = [UIImage imageWithContentsOfFile:path];

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);

imageView.image = [UIImage imageWithCGImage:masked];

And it works great, but the resulting image has BLACK where it was masked out, how can I set it to have WHITE where its masked out?

A: 

The image is supposed to be transparent where it's "masked out"; the colour you see will depend on what background you're drawing it on.

(I don't remember if there's a requirement that the source image has an alpha channel.)

It may be worth making sure that imageView.opaque = NO.

tc.
I set the imageView background's alpha to 0 and opaque to NO but still get black, I wonder if the blackness is at the CGImage level, like I have to draw it to a context.
Shizam
Shizam: the "blackness" is whatever is under the image view then. or if it's transparent, whatever's under that. it's turtles all the way down!
rpetrich
Nah, as a test I created png with transparency and put it in that imageView and it showed through to the background color (blue) but if I put the resulting masked image in there its still black. I went through a longer operation to create a bitmapContext and draw the image into it and THEN use the resulting context as the source for an image, that did correctly have alpha punched through it. Weird.
Shizam
Yeah, very weird (it should be hidden if alpha is 0). Perhaps using a CGImage-with-mask as "layer content" isn't supported?
tc.