0

I'd like to append UIBezierPath without antialiasing. Here is what I tried. testView's frame is 10x10

    let shadowLayer = CALayer()
    var path: UIBezierPath!
    
    shadowLayer.frame = testView.bounds
    path = UIBezierPath(rect: CGRect(x: 1, y: 1, width: 3, height: 3))
    shadowLayer.shadowPath = path.cgPath
    shadowLayer.shadowColor = UIColor.red.cgColor
    shadowLayer.shadowOffset = .init(width: 0, height: 0)
    shadowLayer.shadowOpacity = 1
    shadowLayer.shadowRadius = 0
    testView.layer.addSublayer(shadowLayer)

The red rectangle inside testView is 3x3, no problem.

But when I append another UIBezierPath.

    ...
    path = UIBezierPath(rect: CGRect(x: 1, y: 1, width: 3, height: 3))
    path.append(UIBezierPath(rect: CGRect(x: 5, y: 5, width: 3, height: 3)))
    ...

The red rectangles are getting antialiasing.

How do I disable antialiasing when appending UIBezierPath? Thanks.

  • Have a look at: https://stackoverflow.com/a/15418265/1619193 - it's objective C but talks about what you want – Shawn Frank Mar 20 '22 at 17:21
  • @ShawnFrank Thank you, I checked this post and using NSGraphicsContext and set shouldAntialias does remove antialias, but I still want to know if it is possible to remove antialias when set layer.shadowPath with merged path. – tonypeng2005 Mar 21 '22 at 02:45

1 Answers1

0

You can create a new graphics context, set the shouldAntialias attribute to false and add the cgPath from your UIBezierPath to it. Or you can move it to draw(_:), get the current context and do the same.

https://developer.apple.com/documentation/appkit/nsgraphicscontext/1529486-shouldantialias

Lalo
  • 383
  • 2
  • 8
  • Thank you, using NSGraphicsContext and set shouldAntialias does remove antialias, but I still want to know if it is possible to remove antialias when set layer.shadowPath with merged path. – tonypeng2005 Mar 21 '22 at 02:46