【发布时间】:2014-02-20 22:22:31
【问题描述】:
我创建了一个绘图应用程序,可以在您触摸屏幕时进行绘图,它在模拟器中运行非常流畅,但是当我在 iPad 2 上对其进行测试时,它会变慢并且需要很长时间才能绘制我正在使用这些线条代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:self.view.frame];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 7.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[drawImage setFrame:self.view.frame];
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[self.view addSubview:drawImage];
}
我认为问题在于在 touchMoved: 方法中调用 drawInRect: 方法
【问题讨论】:
-
尝试使用 Time Profiler 工具进行分析 - 它会告诉您哪些方法调用使用的处理时间最多。观看“Core Animation Essentials”WWDC 视频:developer.apple.com/videos/wwdc/2011/index.php
-
这似乎是一个使用 displayLink 的好应用。无论发生多少触摸事件,每秒仅调用 setNeedsDisplay 多次。
-
是的,我知道 drawInRect: call 中的错误,因为当我删除它时它工作顺利,但在这种情况下绘图并不完美