CALayer Antialiasing not as good as UIView antiali

2020-05-19 00:22发布

问题:

I've been trying to animate circle drawing using CALayer. It all works well, but the problem is - drawn circle is not antialiased enough. It has a bit too rough borders, (or blurred if rasterize is used). (AntiAliasing is enabled)

Tried also:

edgeAntialiasingMask = kCALayerLeftEdge | kCALayerRightEdge | kCALayerBottomEdge | kCALayerTopEdge;

to no avail.

Here is an example how it looks like without rasterization:

And here is an example with rasterization: (tried values from 1.0 till 4.0 (just to be sure. Result - the same.))

And here is the same circle, but drawn inside UIView drawrect:

You can see, that circle drawn using UIView drawrect is looking much better.

The reason I cannot use UIView is because I need to animate circle filling. Using CALayer it is really easy, but to do the same on UIView, I don't really know if it is even possible. (I could try to launch drawrect: every 1/60 seconds, but I think it will get laggy, as it is not intended that way).

So - does anyone have any solution how I could make drawn circles/lines on CALayer look the same as drawn on UIView?

回答1:

I've had issues with pixelated drawing in a CALayer on retina devices before. (I'm assuming you're seeing the issue on retina devices) Doing the following fixed the issue I was experiencing:

layer.contentsScale = [[UIScreen mainScreen] scale];

You shouldn't need to deal with rasterization or antialiasing. In my own code, I had initially implemented drawing something to a UIView that I later changed to be drawn in a CALayer, and simply setting the contentsScale property made both draw identically.