我正在實現以下圖形drawRect函數,但它使用了超過50%的CPU - 關於如何解決這個問題的任何想法?我只畫了幾條隨機線,但我希望它們都有不同的寬度。使用太多CPU的核心圖形
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
@autoreleasepool {
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
float width = rect.size.width;
int nbLine=10; // i want to draw 10 paths
for (int iLine=1;iLine<nbLine;iLine++){
float Pathwidth=0.8*(nbLine-(float)iLine)/nbLine;
CGContextBeginPath(context);
CGContextSetLineWidth(context, Pathwidth); //each path should have its own width
CGPathMoveToPoint(path, NULL, 0,0);
for (int i=0;i<10;i++){
float x=width/(i+1);
float y=1;//for this example, I just put a fixed number here - it's normally an external variable
CGPathAddQuadCurveToPoint(path, NULL, x+width/10, y, x,0);
}
CGContextAddPath(context, path);
CGContextStrokePath(context);
}
CGPathRelease(path);
}
}
謝謝!
繪圖應該最大化CPU。圖紙需要多長時間? –
是否有你擁有'@ autoreleasepool'的理由? – Gavin
@autoreleasepool沒有理由(只是一個測試)。它的繪製速度非常快,並且在每個週期都正確更新(我相信每4ms) –