CAShapeLayer croping image

I am trying to crop an image using CAShapeLayer using 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 doing this, I see only an empty view;

+3
source share
1 answer

You never install frameyours contentLayer, so by default it is equal CGRectZero, which makes its contents invisible. Set the frame size to the size of your image and you will see it.

+4
source

All Articles