【问题标题】:Coregraphics causing big lag on iPad?Coregraphics 在 iPad 上造成很大的延迟?
【发布时间】:2011-09-26 23:10:10
【问题描述】:
 UIGraphicsBeginImageContext(self.view.bounds.size);
        [currentStrokeImageView.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), dWidth);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, 1.0f);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), pointA.x, pointA.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), pointB.x, pointB.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        currentStrokeImageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

由于某种原因,这在 iphone/ipod 上运行绝对没有延迟,但在 iPad 上,绘图时出现明显的延迟。我使用的代码在上面,有什么建议可以解决这个问题吗?

【问题讨论】:

  • 了解上面的代码在什么上下文(rimshot!)中执行会很有帮助。
  • 你在什么情况下是什么意思?当用户拖动手指时,它在 self.view 内,请详细说明,因为我不太清楚你的意思(有点初学者)
  • 这段代码是通过什么方法调用的?

标签: iphone memory core-graphics lag


【解决方案1】:

之所以如此滞后,是因为您是在touchesMoved:withEvent: 中进行的。在接收到触摸事件时,可以(显然)多次调用此方法。因为绘制到图形上下文可能是一项资源密集型操作,所以我建议不要做你正在做的所有事情。我会尽可能将您正在执行的渲染推迟到 touchesBegintouchesEnd 方法。如果这不可能,也许您只能在运动中达到某个增量后执行这些操作,例如,每2.0f 个点。

【讨论】:

  • 我已经尝试过 delta 方法(使用距离公式)和计时器方法来尝试让它工作。 delta 方法的问题在于移动的触摸总是至少一个,因此对于精确绘制,delta 方法不会改变任何东西。使用计时器方法,它不会更新足够多的时间和足够快的速度来跟上触摸(甚至是 250.0f/1.0f、300.0f/1.0f)。所以你知道任何其他的解决方案。感谢前两个,但如果您知道另一个,请回复
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多