我看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.
这句话要怎么理解呢?
我看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.
这句话要怎么理解呢?
2 回答1.3k 阅读
1 回答1.4k 阅读✓ 已解决
1 回答1.1k 阅读
1 回答1k 阅读
545 阅读
在Xcode搜索CGContextSetLineWidth的文档,上面有提到:
就是说如果设置宽度为1个像素的线,那么绘制线条分别在所绘路径的两边,每边各一半宽度,即0.5像素。而正在在屏幕上绘制时,是以像素为单位绘制的,并且对于显示内容不满一个像素时,为了平滑显示内容默认使用了抗锯齿效果。如下面中间图形所示,对于1个像素宽度蓝色的线条,左右个0.5像素,剩下0.5像素使用抗锯齿,用淡蓝色绘制。
要解决这个问题,你可以让你的path偏移0.5像素,或者关闭抗锯齿:
CGContextSetAllowsAntialiasing(contextRef,NO);
参考答案来源:
Quartz 2D - 绘线
Core Graphics 101: 线,矩形和渐变效果