Question

I have a UIBezierPath made with 6 vertices. Now I want to get those vertices from the UIBezierPath. Is there any possible way to do it?

Was it helpful?

Solution

You could do this using CGPathApply and CGPathApplierFunction.

NSMutableArray *thePoints = [[NSMutableArray alloc] init];
UIBezierPath *aPath;
CGPath theCGPath = aPath.CGPath;
CGPathApply(theCGPath, thePoints, MyCGPathApplierFunc);

What it does is pass on the Applier Function to each of the path's elements. You can get a list of points from each element inside your function and add them to thePoints.

Your applier function would look something like

void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
    NSMutableArray *thePoints = (NSMutableArray *)info;

    CGPoint *points = element->points;
    [thePoints addObject:[NSValue valueWithCGPoint:points[0]]]; 
    \\Only 1 point assuming it is a line 
    \\Curves may have more than one point in points array. Handle accordingly.

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top