I need to create mask based on image (any possible shape). I need to:
- Create CALayer with CGPath as a proper mask
- Call [UIView.layer setMask: with layer above ]
So main big question is how to set CGPath based on black-white image? Or is it possible to place mask directly from image?
Thanks!
Example image of a mask:
You will need to create your image with an alpha channel. According to
CALayer
's mask property documentation:So the layer you want to use as mask needs an alpha channel. If you want to use an image to create such a channel, the image itself needs to have an alpha channel. So you would need to copy the black and white mask you posted above into the alpha channel of an image. The color of that particular image is ignored. You then load the image, set a layers contents to it and use that layer as a different layers mask. Something around this:
Coded in the browser, excuse systactical errors.
Storing all your masks as png files might blow your app size. You could store the masks as jpgs and create the appropriate alpha version at runtime using the CoreGraphics drawing functions. But that is another question.....
Very similar issue I think to something I did a while back. Basically, you'll need to export the file from Adobe Illustrator then run a routine to convert that file into a CGPath.
You'll find how to do it in my answer to this question:
How to detect/handle touch events on curved regions?