我在iPad上的drawRect方法的Cocoa视图中绘制了几个CGPaths
。我开始直接将它们绘制到UIGraphicsGetCurrentContext()
上下文中,但是当我的路径变得很长时,性能会向南移动。根据{{3}}其他问题,我开始研究使用CGLayer
s。
所以我现在要做的是在调用CGContextRef
的{{1}}内部渲染一条路径。以下是我正在做的基本概要:
CGLayerGetContext
现在,我获得了良好的性能绘图,但我的线条非常锯齿状,似乎没有抗锯齿。我试过添加
// 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
中的线条流畅?
答案 0 :(得分:2)
我发现第二张图片没有消除锯齿的原因是因为没有任何反别名。背景是空的,因此抗锯齿不起作用。如果在视图的边界上创建一个完全透明的大矩形,则该线将使用消除锯齿进行绘制。然而,表现被杀死了。最好不要走这条路。