Reputation: 81
I tried to add a mask layer on a UIImageView. This imageview hold an image and the mask layer hold another image by setting its contents. But when I add the mask layer onto the layer of imageview, the mask layer doesn't hold any image. The mask layer had been initialized by getter. The method adding mask layer is below:
- (void)circleButtonClicked{
self.maskLayer.contents = (id)[UIImage imageNamed:@"mask.jpg"].CGImage;
self.maskLayer.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_WIDTH);
self.maskLayer.opacity = 0.5;
[self.imageView.layer addSublayer:self.maskLayer];
UIBezierPath * pPath = [UIBezierPath bezierPathWithArcCenter:self.imageView.center radius:(kSCREEN_WIDTH-100)/2 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
self.maskLayer.path = pPath.CGPath;
UIBezierPath * otherPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_WIDTH)];
self.maskLayer.path = otherPath.CGPath;
[otherPath appendPath:pPath];
self.maskLayer.path = otherPath.CGPath;
self.maskLayer.fillRule = kCAFillRuleEvenOdd;
}
Upvotes: 2
Views: 406
Reputation: 17722
Shape layers are used for displaying paths not images. There are three different ways to provide layer content (see Core Animation Programming Guide) but only one at a time.
There are some things in your code which I don't understand:
maskLayer
is just a normal layer and not a mask. When you want to use it as mask, you should use the mask
property of CALayer
and not add it as sublayer.imageView
.Upvotes: 1