CGContext Clip Anti Aliasing问题

时间:2014-09-20 15:50:07

标签: ios uiview core-graphics

我在UIView drawRect函数中绘制一个形状,该函数涉及剪切路径,然后在后面添加彩色块,以便颜色具有剪切路径的形状。然而,由于某种原因,路径的线条并不顺利;如果抗锯齿不能正常工作。

CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
CGContextSetLineWidth(context, 1.0);
CGContextSetShouldAntialias(context, true);
CGContextSetAllowsAntialiasing(context, true);
CGContextBeginPath (context);
CGContextMoveToPoint(context, xStart, yStart);
for (int i=0; i<points.count; i++) {
        CGContextAddLineToPoint(context, xPoint, yPoint);
    }

}
CGContextAddLineToPoint(context, xStart, yStart );
CGContextClip(context);

CGRect colorRect = CGRectMake(0, 0 , rectWidth, rectHeight);
CGContextSetFillColorWithColor(context, blockColor.CGColor);
CGContextFillRect(context, colorRect);
CGContextRestoreGState(context);

结果应该有平滑的线条,但是它出现了与可见像素的锯齿状,如下图所示:

Image of the jagged line

知道问题是什么以及如何解决它?

提前致谢

1 个答案:

答案 0 :(得分:0)

奥基。所以,你的代码没有问题。问题是抗锯齿效果有点不同。您绘制一个垂直斜率,期望生成的直方图的边缘将被平滑。但实际上,抗锯齿并不会使得到的数字平滑。它一个接一个地处理路径元素(曲线,线)。因此,例如,如果绘制圆形,其边缘将被平滑。

您的问题有一个简单的解决方案:只需创建一条曲线,包围直方图。将其添加到您的上下文中。它看起来会更顺畅。

抱歉英文不好。