CoreGraphics画线时模糊的问题

我看CoreGraphics的教程时,里面提到1像素绘图模糊的问题,需要偏移0.5点才行。这是为什么呢?stroke path不是stroke在边界上吗?

It turns out that when Core Graphics strokes a path, it draws the stroke on the middle of the exact edge of the path.

这句话要怎么理解呢?

阅读 9k
1 个回答

在Xcode搜索CGContextSetLineWidth的文档,上面有提到:

The default line width is 1 unit. When stroked, the line straddles the path, with half of the total width on either side.

就是说如果设置宽度为1个像素的线,那么绘制线条分别在所绘路径的两边,每边各一半宽度,即0.5像素。而正在在屏幕上绘制时,是以像素为单位绘制的,并且对于显示内容不满一个像素时,为了平滑显示内容默认使用了抗锯齿效果。如下面中间图形所示,对于1个像素宽度蓝色的线条,左右个0.5像素,剩下0.5像素使用抗锯齿,用淡蓝色绘制。

绘制方式

要解决这个问题,你可以让你的path偏移0.5像素,或者关闭抗锯齿: CGContextSetAllowsAntialiasing(contextRef,NO);

参考答案来源:

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题