我正在iPad上的Cocoa视图中的drawRect方法中绘制多个CGPaths。一开始我直接将它们绘制到UIGraphicsGetCurrentContext()上下文中,但是当我的路径变得非常长时性能变差。根据其他问题several的建议,我开始研究使用CGLayer。
所以现在我做的是在调用CGLayerGetContext获取的CGContextRef内部渲染路径。以下是我正在做的基本概述:
现在,我的绘图性能很好,但是我的线条非常锯齿状,似乎没有抗锯齿效果。我已经尝试过添加。
所以现在我做的是在调用CGLayerGetContext获取的CGContextRef内部渲染路径。以下是我正在做的基本概述:
// rect comes from the drawRect: method
layer = CGLayerCreateWithContext(context, rect.size, NULL);
layerContext = CGLayerGetContext(layer);
CGContextSetLineCap(layerContext, kCGLineCapRound);
// Set the line styles
CGContextSetLineJoin(layerContext, kCGLineJoinRound);
CGContextSetLineWidth(layerContext, 1.0);
CGContextSetStrokeColorWithColor(layerContext, [UIColor blackColor].CGColor);
// Create a mutable path
path = CGPathCreateMutable();
// Move to start point
//...
for (int i = 0; i < points.count; i++) {
// compute controlPoint and anchorPoint
//...
CGPathAddQuadCurveToPoint(path,
nil,
controlPoint.x,
controlPoint.y,
anchorPoint.x,
anchorPoint.y);
}
// Stroke the path
CGContextAddPath(layerContext, path);
CGContextStrokePath(layerContext);
// Add the layer to the main context
CGContextDrawLayerAtPoint(context, CGPointZero, layer);
现在,我的绘图性能很好,但是我的线条非常锯齿状,似乎没有抗锯齿效果。我已经尝试过添加。
CGContextSetShouldAntialias(layerContext, YES);
CGContextSetAllowsAntialiasing(layerContext, YES);
CGContextSetInterpolationQuality(layerContext, kCGInterpolationHigh);
对于上述代码,我尝试了各种方法仍无效。我甚至在主要的context
上设置了抗锯齿属性,但没有改变。我截了相同代码结果的屏幕截图,但第二个图像是从CGLayer
中绘制创建的。如你所见,它非常锯齿,尽管是相同的代码,只是绘制到layerContext
中。我应该如何使CGLayer
中的线条光滑?