CAShapeLayer绘制路径奇怪

Dex*_*erW 2 iphone cocoa-touch drawing ipad ios

我正在构建一个UIBezierPath,如下所示:

UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat yOrigin = 2;
for (int i = 0; i < lines; ++i) {
    [path moveToPoint:CGPointMake(0, yOrigin)];
    [path addLineToPoint:CGPointMake(width, yOrigin)];
    yOrigin += distance;
}
[path closePath];
Run Code Online (Sandbox Code Playgroud)

然后我把它放在CAShapeLayer中:

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
[shapeLayer setFrame:aFrame];
[shapeLayer setPath:[bezierPath CGPath]];
[shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
[shapeLayer setMasksToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

但是,当添加到我的滚动视图时,结果如下:

路径图像

为什么这样做?前四行几乎是灰色的!它看起来像一个别名问题,但路径是如此简单.

ton*_*lon 5

是的,这是一个抗锯齿问题.

您使用的路径宽度为1磅.在上面的示例中,CAShapeLayer的上三个实例精确地定位在整个点值上,例如.5.0,其中较低实例位于两个整点之间,例如.14.5.

当系统将您的图层渲染到屏幕像素时,它会对前三行进行抗锯齿处理,以50%的透明度绘制两个触摸的像素.下面的线完美地坐在一个像素上,因此绘制成完美的黑色.

我希望一点点ASCII艺术有助于理解.

 pt          upper lines                           lower line
5.0|---------                                      -------------------------------
   |  pixel  --------------------------------------  perfectly aligned center 5.5
6.0|---------  line between pixels centered on 6.0 -------------------------------
   |  pixel  --------------------------------------
7.0|---------
Run Code Online (Sandbox Code Playgroud)