【问题标题】:UIBezierPath too many paths = too slow?UIBezierPath 太多路径 = 太慢?
【发布时间】:2012-10-12 08:36:51
【问题描述】:

我有一个循环,我在其中向 UIBezierPath 添加了许多 (10000+) 行。这似乎很好,但是一旦我尝试渲染 bezierpath,我的设备就会变得非常缓慢和生涩。这是因为我在路径中添加了太多行吗?

向 UIBezierPath 添加行 - 简化:(这看起来不错)

[path moveToPoint:CGPointZero];

   for (int i = 0; i < 10000; i++ ) {      
           [path addLineToPoint:CGPointMake(i, i)];
    }

渲染 BeizerPath(由 Rob 建议)- 这似乎很慢。

- (void)drawBezierAnimate:(BOOL)animate
{
    UIBezierPath *bezierPath = path;

    CAShapeLayer *bezier = [[CAShapeLayer alloc] init];

    bezier.path          = bezierPath.CGPath;
    bezier.strokeColor   = [UIColor blueColor].CGColor;
    bezier.fillColor     = [UIColor clearColor].CGColor;
    bezier.lineWidth     = 2.0;
    bezier.strokeStart   = 0.0;
    bezier.strokeEnd     = 1.0;
    [self.layer addSublayer:bezier];

    if (animate)
    {
        CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        animateStrokeEnd.duration  = 100.0;
        animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
        animateStrokeEnd.toValue   = [NSNumber numberWithFloat:1.0f];
        [bezier addAnimation:animateStrokeEnd forKey:@"strokeEndAnimation"];
    }
}

问:

1) 这是因为我太快地添加了太多路径吗?

2) 我想最终绘制许多不同颜色的不同线条,所以我假设我需要创建多个 (10000+) UIBezierPaths - 这会帮助还是大大减慢设备速度?

3) 我该如何解决这个问题?

提前感谢您的帮助。

【问题讨论】:

  • 哦,我猜还有一个问题:如果我将代码的绘图部分包含在 drawRect 中会有所不同吗?
  • 也许你应该尝试使用 OpenGL?

标签: iphone ios xcode quartz-graphics drawrect


【解决方案1】:

我认为没有办法让这个运行得足够快。

它可能适用于一次性渲染(到图像缓冲区)。但是有动画 - 不。

文档没有限制路径中的点数,但我认为您已经超过了任何合理的限制。

OpenGL 在这里可能有用 - 也许有人可以插话并就此提供一些建议。

单独使用 UIKit,您可能必须从多个缓存层组合图像。

【讨论】:

  • 这很有道理 - 我将看看 Open GL。谢谢!
【解决方案2】:

我发现 UIBezierPath 由于抗锯齿而运行缓慢。我的解决方案是在各种拖动操作期间关闭抗锯齿,并在手势完成后重新绘制并启用抗锯齿。

您可以使用 CGContextSetShouldAntialias(context, ...); 来做到这一点

【讨论】:

  • 渲染速度更快,但质量较低,1000+点仍然是最大值。
猜你喜欢
  • 2013-04-04
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
  • 2012-04-13
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多