在非变换坐标中获取CALayer的“遮罩路径”

时间:2014-02-16 23:19:40

标签: macos cocoa core-animation calayer quartz-graphics

在Mac上的CoreAnimation中有没有办法获得一个基本上是CALayer的“实际像素范围”或“遮罩路径”的bezier路径?

例如,我有这个CALayer,其照片集为内容,1px白色边框,以及X和Y旋转变换。有没有办法在应用变换的情况下导出像素的路径?

示例图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

我想出了如何满足我的需求。它不是真正的“掩码路径”,但它是参数层坐标空间中变换矩形的路径,这正是我所需要的。

此方法的上下文是CALayer上的一个类别:

- (NSBezierPath*)layerPathConvertedToLayer:(CALayer*)toLayer
{
    CGRect bounds = self.bounds;
    CGPoint topLeft = CGPointMake(NSMinX(bounds), NSMinY(bounds));
    CGPoint topRight = CGPointMake(NSMaxX(bounds), NSMinY(bounds));
    CGPoint bottomRight = CGPointMake(NSMaxX(bounds), NSMaxY(bounds));
    CGPoint bottomLeft = CGPointMake(NSMinX(bounds), NSMaxY(bounds));

    CGPoint convertedTopLeft = [self convertPoint:topLeft toLayer:toLayer];
    CGPoint convertedTopRight = [self convertPoint:topRight toLayer:toLayer];
    CGPoint convertedBottomRight = [self convertPoint:bottomRight toLayer:toLayer];
    CGPoint convertedBottomLeft = [self convertPoint:bottomLeft toLayer:toLayer];

    NSBezierPath *bezierPath = [NSBezierPath bezierPath];
    [bezierPath moveToPoint:convertedTopLeft];
    [bezierPath lineToPoint:convertedTopRight];
    [bezierPath lineToPoint:convertedBottomRight];
    [bezierPath lineToPoint:convertedBottomLeft];
    [bezierPath lineToPoint:convertedTopLeft];
    [bezierPath closePath];

    return bezierPath;
}