CGLayer和抗锯齿CGPath

4
我正在iPad上的Cocoa视图中的drawRect方法中绘制多个CGPaths。一开始我直接将它们绘制到UIGraphicsGetCurrentContext()上下文中,但是当我的路径变得非常长时性能变差。根据其他问题several的建议,我开始研究使用CGLayer。
所以现在我做的是在调用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中的线条光滑?

smooth lines jagged lines

1个回答

3
我发现第二张图没有抗锯齿的原因是没有可抗锯齿的对象。背景是空白的,因此无法进行抗锯齿处理。如果你在视图边界上创建一个完全透明的大矩形,线条就会带有抗锯齿效果。但是,这将导致性能下降。最好不要使用这种方法。

嗨@Streeter,我也遇到了CgLayer的问题,不知道如何绘制到CgLayer上,我已经发布了一个相关问题,请你帮忙看一下:http://stackoverflow.com/questions/11341763/how-to-use-cglayer-for-optimal-drawing。 - Ranjit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接