我有一個模型,其中包含用戶繪製的所有線條的NSMutableArray
curvedPaths
。線本身也是包含點對象的NSMutableArray
。當我繪製彎曲的NSBezier
路徑時,我的點數組具有以下結構:linePoint,controlPoint,controlPoint,linePoint,controlPoint,controlPoint等...我認爲有一個數組包含所有點和控制點會比處理2或3個不同的陣列。在一個簡單的繪圖應用程序中優化視圖繪圖代碼
很明顯,我的視圖繪製了它從模型中獲得的路徑,這導致了一個實際問題:有沒有一種方法可以在速度方面優化以下代碼(在視圖的drawRect方法中)?
int lineCount = [[model curvedPaths] count];
// Go through paths
for (int i=0; i < lineCount; i++)
{
// Get the Color
NSColor *theColor = [model getColorOfPath:[[model curvedPaths] objectAtIndex:i]];
// Get the points
NSArray *thePoints = [model getPointsOfPath:[[model curvedPaths] objectAtIndex:i]];
// Create a new path for performance reasons
NSBezierPath *path = [[NSBezierPath alloc] init];
// Set the color
[theColor set];
// Move to first point without drawing
[path moveToPoint:[[thePoints objectAtIndex:0] myNSPoint]];
int pointCount = [thePoints count] - 3;
// Go through points
for (int j=0; j < pointCount; j+=3)
{
[path curveToPoint:[[thePoints objectAtIndex:j+3] myNSPoint]
controlPoint1:[[thePoints objectAtIndex:j+1] myNSPoint]
controlPoint2:[[thePoints objectAtIndex:j+2] myNSPoint]];
}
// Draw the path
[path stroke];
// Bye stuff
[path release];
[theColor release];
}
感謝, xonic
偉大的一點在那裏!保存路徑並不在我的腦海中,但實際上是一個非常好的主意。事情是,我在整個桌面上都有一個透明的視圖,以便讓用戶在各種窗口上畫畫,或者寫下筆記等等。所以路徑不會經常改變,但是經常被拖動,並且用戶可以擦除一個或多個路徑。我認爲紋理不會完成這項工作。但是現在我已經發現了NSBezierPath的containsPoint:方法,它甚至會優化我的'擦除路徑代碼'! Thx很多老兄! – xon1c 2010-05-26 10:26:21