3

I am trying to crop an image using CAShapeLayer with this code

    CALayer* contentLayer = [CALayer layer];
    CAShapeLayer* mask = [CAShapeLayer layer];

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 10, 10);
    CGPathAddLineToPoint(path, NULL, 10, 80);
    CGPathAddLineToPoint(path, NULL, 80, 80);
    CGPathAddLineToPoint(path, NULL, 80, 10);
    mask.path = path;

    [contentLayer setContents:(id)[[UIImage imageNamed:@"image.png"] CGImage]];
    [contentLayer setMask:mask];          

    [[self layer]addSublayer:contentLayer];

After executing this I can see just empty view;

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vitalii Boiarskyi
  • 1,363
  • 2
  • 10
  • 25

1 Answers1

4

You never set the frame of your contentLayer, so it defaults to CGRectZero, which makes its contents invisible. Set the frame to have the size of your image and you should see it.

omz
  • 53,243
  • 5
  • 129
  • 141