我创建了一个绘图应用程序,可以在触摸屏幕时进行绘图,它在模拟器中运行非常流畅,但是当我在 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: 方法
最佳答案
- 在您的 touchesMoved 中构建一个 UIBezierPath 并且不要在那里进行任何绘图。
- 在 touchesMoved 中调用 setNeedsDisplay。
- 覆盖 drawRect 并在那里绘制路径。
或
将 View 的图层背景设置为 CAShapeLayer 并将点添加到 touchesMoved 中的图层。
关于ios - 绘图应用程序性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920997/