Pages

Finding the Actual Bounds of a CGPath/UIBezierPath

If you’re one of the admittedly few people for whom CGPathGetBoundingBox and CGPathGetPathBoundingBox aren’t accurate enough, you’ve come to the right place.

Both CGPathGetBoundingBox and its younger brother CGPathGetPathBoundingBox are designed to quickly find a box surrounding a given CGPath, and they take two distinct approaches to doing so. (If you’re more of a UIBezierPath person, know that the bounds property of a path is computed internally using CGPathGetPathBoundingBox — but you can access the path’s CGPath property to compute the bounding box using the method provided here instead).

As an example, take the CGPath formed by the following:

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 60, 100);
CGPathAddQuadCurveToPoint(path, NULL, 170, 30, 280, 100);
CGPathAddLineToPoint(path, NULL, 240, 200);
CGPathAddCurveToPoint(path, NULL, 140, 240, 120, 240, 20, 200);
CGPathCloseSubpath(path);

Which creates the following simple path:

CGPathPlain

So let’s say we’re trying to find a bounding box for that path.

Continue Reading