【问题标题】:CGLayer and Anti-aliased CGPathsCGLayer 和抗锯齿 CGPaths
【发布时间】:2011-08-06 00:05:11
【问题描述】:

我在 iPad 上的 drawRect 方法中的 Cocoa 视图中绘制了几个 CGPaths。我开始将它们直接绘制到UIGraphicsGetCurrentContext() 上下文中,但是当我的路径变得很长时,性能下降了。基于several 其他问题,我开始研究使用CGLayers。

所以我现在要做的是在CGContextRef 内渲染一条路径,我通过调用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。如何让CGLayer 中的线条流畅?

【问题讨论】:

    标签: iphone cocoa ipad quartz-graphics quartz-2d


    【解决方案1】:

    我发现第二张图片没有抗锯齿的原因是因为没有什么可以抗锯齿的。背景是空的,所以抗锯齿不起作用。如果您在完全透明的视图边界上制作一个大矩形,则该线将使用抗锯齿绘制。然而,性能被扼杀了。最好不要走这条路。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2016-06-28
    • 2011-07-05
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多