【发布时间】: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