在Mac上的CoreAnimation中,有没有一种方法可以获取基本上是CALayer的“实际像素边界”或“掩码路径”的贝塞尔曲线?
例如,我有一个带有照片集的CALayer,其内容为1px白色边框以及X和Y旋转变换。有没有一种方法可以应用变换来导出其像素的路径?
示例图片:
最佳答案
我想出了如何满足我的需求的方法。它不是真正的“掩码路径”,而是参数层的坐标空间中转换后的矩形的路径,而这正是我所需要的。
此方法的上下文是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;
}