3

The doucumet said that the CAShapLayer is anti-aliased ,why got this result. I also draw a circle with the CGContext in the drawRect method,It's very perfect.

   UIBezierPath *path = [UIBezierPath bezierPath];
    [path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(150, 300, 136, 136)]];

    self.circleLayer = [CAShapeLayer layer];
    self.circleLayer.path = path.CGPath;
    self.circleLayer.frame = self.bounds;
    self.circleLayer.fillColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6].CGColor;
    [self.layer addSublayer:self.circleLayer];

enter image description here

Luke
  • 370
  • 4
  • 11
  • Are you asking us how to turn off anti-aliasing? Or is your Core Graphics rendition also doing anti-aliasing, and you're just wondering why the two differ a little? – Rob Aug 28 '13 at 16:45
  • BTW, I find that if you use `UIBezierPath` method `addArcWithCenter` with non-integer radius (e.g 150.2 vs 150), the artifacts of `CAShapeLayer`'s anti-aliasing is less distracting. – Rob Aug 28 '13 at 17:29

3 Answers3

2

According to the UIBezierPath docs, bezierPathWithOvalInRect: "creates a closed subpath that approximates the oval using a sequence of Bézier curves," so it may not draw a perfect circle.

Brigham
  • 14,395
  • 3
  • 38
  • 48
  • But [CGContextAddEllipseInRect](https://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextAddEllipseInRect) also said :" The ellipse is approximated by a sequence of Bézier curves." – Luke Aug 29 '13 at 01:53
  • 1
    When I use CGContextAddEllipseInRect draw a circle in the drawRect ,It's a perfect circle. – Luke Aug 30 '13 at 01:41
1

If you use a UIBezierPath to draw a circle will not a perfect circle because its an approximation from Bézier curves.

You can draw a perfect circle from a square UIView. For example:

myView.layer.cornerRadius = myView.frame.size.width/2;

In this topic explain three methods to draw a circle -> How to draw a smooth circle with CAShapeLayer and UIBezierPath?

Community
  • 1
  • 1
MarMass
  • 5,755
  • 1
  • 18
  • 15
0

iOS 12: I had the same issue on iPhone XS and XS Max. I don't know why, but on iPhone 8 there was no issue. The circle was perfectly round.

To solve the issue on XS and XS Max I set the flatness property of UIBezierPath to 0.0 and made sure that the center of the rect used for UIBezierPath is either an integer number or a floating number of e.g. 25.5. The center of my rect was y=51.166666 and this caused the issue that the oval was not round.

Baran
  • 2,710
  • 1
  • 23
  • 23