Search code examples
ioscalayermask

Can you use a grayscale image with no alpha as a layer mask?


Normally if you install an image as the contents of a layer's mask layer, the alpha channel of the mask layer's image is what masks the other layer. I know how to do that.

I have a vague memory of reading that you can instead install a grayscale image with no alpha and use the brightness of the image as the mask. I've tried searching various ways, but can't find any documentation about that.

Am I imagining it?

It would be more memory efficient, and make your code cleaner, if you could draw into a mask image using a shade of gray.


Solution

  • For CALayer, the mask is alpha mask. In CoreGraphics, one can mask with grayscale image. Also, Core Image has CIMaskToAlpha filter which one can use to convert grayscale image to alpha mask.