Search code examples
objective-ccocoansbezierpath

bezierPathWithRoundedRect: -- odd rendering artefact


I am drawing a simple rectangle with a few rounded corners:

UIBezierPath * p = [UIBezierPath bezierPathWithRoundedRect:outline
                                         byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                               cornerRadii:CGSizeMake(8, 8)];

Oddly enough the top left hand corner seems incomplete - a small notch is missing:

Image that shows the flaw in the top left hand corner

Strangely enough - if add a [p close] - that problem goes away. Now the documentation suggests that:

This method creates a closed subpath, proceeding in a clockwise direction (relative to the default coordinate system) as it creates the necessary line and curve segments.

so I am wondering where things go wrong ? Am I misunderstanding the documentation - or is there a subtle bug/issue in my code ?

For completeness the offending code is

- (void)drawRect:(CGRect)rect {
CGRect outline = CGRectInset(self.bounds, 4, 4);
UIBezierPath * p = [UIBezierPath bezierPathWithRoundedRect:outline
                                         byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
                                               cornerRadii:CGSizeMake(8, 8)];
// [p closePath];
[[UIColor blackColor] setStroke];
[p setLineWidth:4];
[p stroke];
....

Solution

  • It looks like the final path isn’t actually closed, which might be a bug (worth filing if so)—basically, since it doesn’t know the beginning and end points are connected, you’re seeing end caps (which by default are drawn only to the exact end of the path) rather than a continuous line. It sounds like your -close solution works; go with that.