Question

Is there a way in CoreAnimation on the Mac to get a bezier path of what is essentially the "bounds of the actual pixels" or "mask path" of a CALayer?

For example, I have this CALayer with a photo set as its content, a 1px white border, and a X and Y rotation transform. Is there a way to derive its pixel's path with the transform applied?

Example image:

enter image description here

Was it helpful?

Solution

I figured out how to do this for my needs. It's not the true "mask path", but it is the path of the transformed rectangle in the coordinate space of the argument layer and that is really what I needed.

The context of this method is a category on 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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top