ios - 绘图应用程序性能

标签 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: 方法中调用drawInRect: 方法

最佳答案

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

将 View 的图层背景设置为 CAShapeLayer 并将点添加到 touchesMoved 中的图层。

关于ios - 绘图应用程序性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920997/

相关文章:

c++ - vswprintf_l vswprintf 的扩展区域设置版本?

iphone - 在 UIWebView 中加载本地 HTML 文件时遇到问题

ios - 应用扩展和主应用之间的共享文件

c# - 如何停止闪烁 C# winforms

objective-c - CG图像记录失真

iphone - 从 UIScrollView + iphone 生成 PDF

iOS 的 LLDB 的 P 命令不打印帧变量

android - 如何在Android上绘制未填充的数字?

iOS 贝塞尔曲线路径显示为之字形

ios - CGContextDrawPath 用于不同颜色的两个内部和外部 Rect