Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAShapeLayer cropping image

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;

like image 485
Vitalii Boiarskyi Avatar asked Dec 04 '25 11:12

Vitalii Boiarskyi


1 Answers

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.

like image 106
omz Avatar answered Dec 06 '25 23:12

omz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!