Search code examples
iosobjective-cmaskcashapelayer

IOS: Cannot Change Color of CAShapeLayer


I want to create a layer to act as a mask for my UIImageView which was created in interface builder. However, no matter what I do the layer remains white. The code is pretty straightforward any ideas what is causing this behavior ?

UIImage* image = [UIImage imageNamed:@"face"];
self.imageView.image = image;

CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = self.imageView.bounds;
maskLayer.fillColor = [[UIColor blackColor] CGColor];
maskLayer.path = CGPathCreateWithRect(self.imageView.bounds, NULL);

self.imageView.layer.mask = maskLayer;
self.maskLayer = maskLayer;

I edited the code and added the path but it still does not work.


Solution

  • You seem to have a misconception as to what a mask is. It is a set of instructions, in effect, for injecting transparency into a view, thus "punching a hole" through the view to a greater or lesser extent (depending on the degree of transparency). It has no color. You are seeing white because that is the color of what is behind your image view — you have punched a hole through the entire image view and made it invisible.