0

I'm trying to apply a mask to both a base layer and a background layer, but the solutions below blows up when setting the mask on either layer. Could anyone please let me know what I'm doing wrong?

self.backLayer = [[CALayer alloc] init];
[self.backLayer setBounds:[self bounds]];
[self.backLayer setPosition:CGPointMake([self bounds].size.width/2, [self bounds].size.height/2)];
self.backLayer.opacity = 0.3f;
[[self layer] insertSublayer:self.backLayer atIndex:0];

[[self layer] setMasksToBounds:YES];
[[self layer] setBorderWidth:0.0f];

UIBezierPath* path = [UIBezierPath bezierPathWithOvalInRect:[self bounds]];
self.backLayer2 = [CAShapeLayer layer];
self.backLayer2.path = path.CGPath;
self.layer.mask = self.backLayer2;
self.backLayer.mask = self.backLayer2;
CGPathRelease(path.CGPath);
Shaeldon
  • 873
  • 4
  • 18
  • 28
Joao
  • 619
  • 1
  • 5
  • 14

2 Answers2

0

Seems your code posted is perfect. There is no problem with the code. The EXC_BAD_ACCESS will occur if you perform any action on deallocated object. Please make sure the self object/view is not deallocated by ARC. Check the place where you declared the view is still alive in stack/memory. Dont forget to check the property declaration parameters (Strong/weak/noatomic) for backLayer property.

Arpit Kulsreshtha
  • 2,452
  • 2
  • 26
  • 52
0

It turns out that you are not supposed to release the CGPath of a UIBezierPath. So, the line

CGPathRelease(path.CGPath);

releases memory the object tries to also release later, causing a memory violation.

Joao
  • 619
  • 1
  • 5
  • 14