views:

216

answers:

1

I have a mask (loaded from a 256 grey PNG) that I want to apply to an image that's being used as part of my process for drawing a UITableViewCell's imageView.image property.

When the cell isn't selected/highlighted, I CGImageCreateWithMask with a square of the proper color and the mask, then drawAtPoint: it into the image I'm building. This works fine.

However, when the cell is selected or highlighted, I'd like to instead use the mask to instead punch through my image appropriately. That is, when my mask specifies full opacity, I want the image I'm building to be completely transparent so the tableview's background is drawn through it. Where my mask specifies 0 opacity, I want the alpha channel untouched. I want nothing other than the alpha channel affected.

I guess what I mean is that I want to draw clearColor over a UIImage, with a varying level of opacity according to a mask.

First, what is this called? And second, how do I do it?

+1  A: 

I think you have to manipulate the CALayers for that. You can use the mask property of the cell's CALayer : CALayer mask attribute.

That is, something in the way of (if myMask is descendent of UIView) :

myCell.layer.mask = myMask.layer
Eino Gourdin
Neither the image nor the mask are, they're both UIImages. And the result must be a UIImage.
Steven Fisher
No, UIImage is a direct descendent of NSObject. You're thinking of UIImageView.
Steven Fisher