CAShapeLayer裁剪图像

时间:2012-03-09 16:09:45

标签: objective-c ios

我正在尝试使用带有此代码的CAShapeLayer裁剪图像

    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];

执行此操作后,我只能看到空视图;

1 个答案:

答案 0 :(得分:4)

您从未设置frame的{​​{1}},因此默认为contentLayer,这使其内容不可见。将框架设置为具有图像大小,您应该看到它。