【发布时间】:2014-02-19 17:38:23
【问题描述】:
我正在实现以下图形 drawRect 函数,但它使用超过 50% 的 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是有原因的吗? -
@autoreleasepool 没有理由(只是一个测试)。它的绘制速度非常快,并且每个周期都会正确更新(我相信每 4 毫秒)
-
周期有什么变化?为什么要重绘每一帧?
-
因为在每个循环中,
float y=1实际上都会更新一个新值(我已经为示例进行了简化)
标签: ios core-graphics core-animation