绘制app性能

时间:2014-02-20 22:22:32

标签: ios drawing cgcontext

我已经创建了一个绘图应用程序,当你触摸屏幕时绘制它在模拟器中非常流畅但是当我在我的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:method

中调用drawInRect:方法

1 个答案:

答案 0 :(得分:1)

  1. 在touchesMoved中构建UIBezierPath,不要在那里进行任何绘图。
  2. 在touchesMoved中调用setNeedsDisplay。
  3. 覆盖drawRect并在那里绘制路径。
  4. OR

    使用将视图的图层背景设置为CAShapeLayer,并将点添加到touchesMoved中的图层。