Bezier曲线绘图非常滞后

时间:2016-06-05 05:23:33

标签: ios objective-c uibezierpath

所以这是发生了什么: 当我画画时,Bezier线非常流畅。我应用了制作控制点和终点的概念,以使平滑成为可能。但是,我找不到导致滞后的问题。

当我画画时,我检查我的CPU使用率,它在5秒内从大约50%变为90%。我确保当我完成绘图时,我的点被删除,同时创建缓冲区以创建我绘制的图像。

我的猜测是在touchesMoved中同时绘制的点太多了?必须有一些东西填充点,这对于程序来说可能太多了。

#import "SmoothedBIView.h"

@implementation SmoothedBIView
{
    UIBezierPath *path;
    UIImage *incrementalImage;
    CGPoint pts[5]; // need to keep track of the four points of a Bezier segment and the first control point of the next segment
    uint ctr;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setMultipleTouchEnabled:NO];
        [self setBackgroundColor:[UIColor whiteColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;

}
/* - (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setMultipleTouchEnabled:NO];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;
}
*/


 animation.
- (void)drawRect:(CGRect)rect
{
    [incrementalImage drawInRect:rect];
    [path stroke];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    ctr = 0;
    UITouch *touch = [touches anyObject];
    pts[0] = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    ctr++;
    pts[ctr] = p;
    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment

        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

        [self setNeedsDisplay];
        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self drawBitmap];
    /*[self setNeedsDisplay]; */
    [path removeAllPoints];
    ctr = 0;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

- (void)drawBitmap
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);

    if (!incrementalImage)
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
        [[UIColor whiteColor] setFill];
        [rectpath fill];
    }
    [incrementalImage drawAtPoint:CGPointZero];
    [[UIColor blackColor] setStroke];
    [path stroke];
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

@end

2 个答案:

答案 0 :(得分:1)

这里有一些问题:

  1. 如果您担心此算法遭受的持续滞后,一个问题是您只在计数器为4时更新路径(并且您始终是至少落后一点),这将夸大懒散。您可以更频繁地更新路径,如此处所述https://stackoverflow.com/a/34997902/1271826,这是一个非常类似于您的Swift实现。

    基本思路是,不是等到你的计数器达到4,而是在计数器为1时画一条线,当计数器为2时画一条四条曲线,当计数器为3时绘制一条三次曲线,当计数器为4时,绘制修正的三次曲线。这样可以减少算法所带来的一般滞后性。

  2. 您可以通过使用预测触摸来减少感知的懒散。它并没有解决问题,(并且使算法稍微复杂化,因为你必须考虑在真正的触摸最终进入时退出先前预测的触摸的概念),但它将感知延迟保持得更多。有关如何使用预测触摸的示例(与Catmull-Rom和Hermite样条曲线相结合,另一种平滑算法),请参阅https://stackoverflow.com/a/34583708/1271826。我为Swift参考道歉,但是如果你搜索Objective-C的预测触摸,我怀疑你也会找到很多例子。

  3. 如果您因贝塞尔变得太长而担心滞后,而不是等到触摸结束后再生成快照,请在经过一定数量的触摸后进行,即使在中间一个手势。这样可以防止你的bezier长时间开始出现性能问题。是的,你不想在绘图手势中间做一些计算密集的事情,但你必须在某个时刻画一条线(没有双关语)。

  4. 有一个论点是使用CAShapeLayer作为bezier路径而不是自己绘制它。我已经看到它表明这比简单的drawRect实施更优化,但我承认我从未对它进行过基准测试。

答案 1 :(得分:0)

性能的轻松胜利是使用setNeedsDisplayInRect:而不是setNeedsDisplay。您当前正在重新绘制整个视图,而不仅仅是已更改的区域。