我正在为iPhone设计绘图应用程序。 在iPhone模拟器中,它可以正常工作约5秒钟,但随着我画的越多,它就会变得越滞后。 当我在设备上对其进行测试时,它变得更加笨拙,甚至无法绘制简单的树。 当我检查处理器在xcode中运行的百分比时,通常在97-100%之间。 有没有什么办法解决这一 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在制作涉及绘图线的多人游戏。 我有10种不同的自定义UIView,其中使用Core Graphics以独特的颜色绘制了一条线。 所有视图彼此堆叠,并且是半透明的。 之所以在相应的UIView中绘制每种颜色而不是一种颜色,是因为我想让用户能够清除特定的视图。 但是,如果我同时绘制所有这些视图,则会发生大量的滞后。 如果我检查时间分析器,则大部分时间都花在绘图上。 但这并没有那么糟糕,也不会造成太大的滞后。 因此,有什么方法可以让我绘制多个UIView(或其他),而不会引起这种滞后。 是我的代码导致了这种滞后,还是UIView?
- (void)drawRect:(CGRect)rect {
//create a image reference from the bitmap and then draw it in the context of the UIView.
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef cacheImage = CGBitmapContextCreateImage(cacheContext);
CGContextDrawImage(context, self.bounds, cacheImage);
CGImageRelease(cacheImage);
}
-(void)draw{
//drawing code. Could ignore the Beizer curve algorithm here.
CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]);
CGContextSetFillColorWithColor(cacheContext, [[UIColor blueColor] CGColor]);
CGContextSetLineCap(cacheContext, kCGLineCapButt);
CGContextSetLineWidth(cacheContext, 6+thickness);
point0 = point1;
point1 = point2; // previous previous point
point2 = pos; // previous touch point
point3 = currentpos;
double x0 = (point0.x > -1) ? point0.x : point1.x; //after 4 touches we should have a back anchor point, if not, use the current anchor point
double y0 = (point0.y > -1) ? point0.y : point1.y; //after 4 touches we should have a back anchor point, if not, use the current anchor point
double x1 = point1.x;
double y1 = point1.y;
double x2 = point2.x;
double y2 = point2.y;
double x3 = point3.x;
double y3 = point3.y;
// Assume we need to calculate the control
// points between (x1,y1) and (x2,y2).
// Then x0,y0 - the previous vertex,
// x3,y3 - the next one.
double xc1 = (x0 + x1) / 2.0;
double yc1 = (y0 + y1) / 2.0;
double xc2 = (x1 + x2) / 2.0;
double yc2 = (y1 + y2) / 2.0;
double xc3 = (x2 + x3) / 2.0;
double yc3 = (y2 + y3) / 2.0;
double len1 = sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
double len2 = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
double len3 = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
double k1 = len1 / (len1 + len2);
double k2 = len2 / (len2 + len3);
double xm1 = xc1 + (xc2 - xc1) * k1;
double ym1 = yc1 + (yc2 - yc1) * k1;
double xm2 = xc2 + (xc3 - xc2) * k2;
double ym2 = yc2 + (yc3 - yc2) * k2;
double smooth_value = 0.8;
// Resulting control points. Here smooth_value is mentioned
// above coefficient K whose value should be in range [0...1].
float ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
float ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
float ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
float ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
CGContextMoveToPoint(cacheContext, point1.x, point1.y);
CGContextAddCurveToPoint(cacheContext, ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, point2.x, point2.y);
CGContextStrokePath(cacheContext);
CGRect dirtyPoint1 = CGRectMake(point1.x-(8+thickness)/2, point1.y-(8+thickness)/2, (8+thickness), (8+thickness));
CGRect dirtyPoint2 = CGRectMake(point2.x-(8+thickness)/2, point2.y-(8+thickness)/2, (8+thickness), (8+thickness));
[self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.