我在一个iPad上的drawRect方法中的Cocoa视图中绘制了几个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中的线条平滑?


发布于 2011-08-17 07:03:06
我发现第二个图像没有抗锯齿的原因是因为没有什么可以抗锯齿的。背景是空的,因此抗锯齿效果不起作用。如果在视图的边界上画一个完全透明的大矩形,则绘制的线条将带有抗锯齿效果。然而,性能却被扼杀了。最好不要走这条路。
https://stackoverflow.com/questions/6963857
复制相似问题