Add translucent mask to CALayer?

I try to darken the surrounding area of ​​my UIImageView and leave the part alone (which I define with my mask).

Now I define my mask and set my imageView.layer.mask, but instead of darkening the rest of the image, it completely removes it.

An example of the type of effect I want: http://i.imgur.com/vVUiuyk.png

An example of what I get: http://i.imgur.com/5DTXo0S.png

Reference documents mention that the mask uses its layer alpha, so I tried to manipulate the opacity of the mask. However, this only affects the opacity of the part that I want to leave alone, while the rest of the image is still completely cut out.

Can someone point out what I'm doing wrong? Thanks.

Here is my code:

CAShapeLayer *mask = [CAShapeLayer layer]; GMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, nil, 1052, 448); CGPathAddLineToPoint(path, nil, 2, 484); CGPathAddLineToPoint(path, nil, 54, 1263); CGPathAddLineToPoint(path, nil, 56, 1305); CGPathAddLineToPoint(path, nil, 380, 1304); CGPathAddLineToPoint(path, nil, 1050, 1311); CGPathCloseSubpath(path); mask.path = path; CGPathRelease(path); //mask.opacity = 0.5; //doesn't affect the surrounding portion, only the cut out area. self.imageView.layer.mask = mask; 
+4
source share
1 answer

What you do wrong uses the layer mask first. You are trying to obscure or darken an area of ​​your image. This is not what a layer mask does at all! Basically, a layer mask makes its way into the existing layer, forcing everything that is behind it to manifest itself. This is exactly what you found:

he completely removes it instead

Yes, because that's what layer masks do! If you do not want this, why do you use a layer mask?

What you want is just to lay the second kind of image (or just a sublevel) on top of the first. It contains the image you are drawing. It is transparent, except when it has a translucent dark color fill. It will be darker than behind. You use a clipping path to identify an area that does not get a dark fill color.

Alternatively, change the image in the image view by drawing it on top using the layout or, possibly, using CIFilter.

+3
source

All Articles