简体   繁体   English

为什么我的Coregraphics绘图代码会导致滞后?

[英]Why does my Coregraphics drawingcode cause lag?

I'm working on an drawing app for iPhone. 我正在为iPhone设计绘图应用程序。 It works fine for like 5 seconds in the iPhone simulator but as more I draw it gets more laggy. 在iPhone模拟器中,它可以正常工作约5秒钟,但随着我画的越多,它就会变得越滞后。 When I test it on the device it gets even more laggy and I can't even draw a simple tree. 当我在设备上对其进行测试时,它变得更加笨拙,甚至无法绘制简单的树。 When I check how high percent the processor is running at in xcode it usually go between 97-100%. 当我检查处理器在xcode中运行的百分比时,通常在97-100%之间。 Is there any way to fix this? 有没有什么办法解决这一问题?

(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {     
    UITouch *touch = [touches anyObject];
    currentPoint = [touch locationInView:self.view];


    UIGraphicsBeginImageContext(CGSizeMake(320, 568));
    [drawImage.image drawInRect:CGRectMake(0, 0, 320, 568)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
    CGContextBeginPath(UIGraphicsGetCurrentContext());

    CGPathMoveToPoint(path, NULL, lastPoint.x, lastPoint.y);
    CGPathAddLineToPoint(path, NULL, currentPoint.x, currentPoint.y);
    CGContextAddPath(UIGraphicsGetCurrentContext(),path);
    CGContextStrokePath(UIGraphicsGetCurrentContext());

    [drawImage setFrame:CGRectMake(0, 0, 320, 568)];
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastPoint = currentPoint;

    [self.view addSubview:drawImage];
}

When running instruments on your method, I got the following results: 使用您的方法运行仪器时,得到以下结果:

在此处输入图片说明

What that tells me: 那告诉我的是:

  • Setting up a new context every time you want to draw something is 每次想要绘制东西时都要设置一个新的上下文
    waste of time. 浪费时间。 consider only setting it up once, store it somewhere, and you save almost a third of the time, the method currently needs 考虑只设置一次,将其存储在某个地方,您节省了将近三分之一的时间,该方法目前需要
  • drawImage is the other most-consuming part. drawImage是另一个最消耗部分。 It will be enough to set that only once! 只需设置一次就足够了!
  • all other calls are almost negligible 所有其他电话几乎可以忽略不计

There's a great talk about graphics performance including a demo and code walkthrough of a drawing app here: 这里有很多关于图形性能的讨论,包括演示和绘图应用程序的代码演练:

https://developer.apple.com/videos/wwdc/2012/ https://developer.apple.com/videos/wwdc/2012/

The iOS App Performance: Graphics and Animations video iOS应用性能:图形和动画视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM