我在iPad的drawRect方法的Cocoa視圖中繪製了幾個CGPaths
。我開始將它們直接繪製到UIGraphicsGetCurrentContext()
上下文中,但是當我的路徑變得很長時,性能下降了。根據several其他問題,我開始考慮使用CGLayer
s。CGLayer和反鋸齒CGPaths
所以我現在要做的是在撥打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
中的線條流暢?
嗨@Streeter,我也堅持與CgLayer isssue,並不能夠繪製如何繪製到CgLayer,我已經發布了一個關於這個問題,你可以請看看http://stackoverflow.com/questions/ 11341763 /如何使用的,cglayer換最佳繪圖 – Ranjit