2011-08-06 94 views
4

我在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中的線條流暢?

smooth lines jagged lines

回答

2

我發現第二個圖像不抗鋸齒是因爲沒有什麼可反對抗混疊的原因。背景是空的,所以反走樣不起作用。如果您在完全透明的視圖邊界上製作一個大矩形,則該線將使用抗鋸齒進行繪製。然而,表演被殺害。最好不要走這條路。

+0

嗨@Streeter,我也堅持與CgLayer isssue,並不能夠繪製如何繪製到CgLayer,我已經發布了一個關於這個問題,你可以請看看http://stackoverflow.com/questions/ 11341763 /如何使用的,cglayer換最佳繪圖 – Ranjit