【问题标题】:Drawing app performance绘图应用程序性能
【发布时间】: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 中的错误,因为当我删除它时它工作顺利,但在这种情况下绘图并不完美

标签: ios drawing cgcontext


【解决方案1】:
  1. 在您的 touchesMoved 中构建一个 UIBezierPath 并且不要在那里进行任何绘图。
  2. 在 touchesMoved 中调用 setNeedsDisplay。
  3. 覆盖 drawRect 并在那里绘制路径。

使用将您的视图的图层支持设置为 CAShapeLayer 并将点添加到 touchesMoved 中的图层。

【讨论】:

  • +1 这是正确的答案。 @user3330895 不要指望任何人为您编写代码;你自己做会学到更多。 Stephen 的回答很清楚——如果你不明白,你应该 a) 阅读你应该如何在视图中绘制,b) 具体询问你不明白的地方。
猜你喜欢
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多