1

I'm working on an drawing app for iPhone. It works fine for like 5 seconds in the iPhone simulator but as more I draw it gets more laggy. 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%. 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]; } 
7
  • 2
    You should externalize your context (define it at a higher level, perhaps as an ivar of your class) and re-use it on every touch. You wouldn't need recreating it over and over again. Commented Feb 25, 2014 at 10:20
  • Also, avoid calling UIGraphicsGetCurrentContext() so frequently. Just call it once and assign the result: CGContextRef ctx = UIGraphicsGetCurrentContext(); Commented Feb 25, 2014 at 10:21
  • 3
    Also, here you're re-adding drawImage on each frame. That might be a big performance hit. Commented Feb 25, 2014 at 10:22
  • could you tell me more in detail. Im pretty new to objective C. Commented Feb 25, 2014 at 10:23
  • "Also you are re-adding draw image for each frame" Yeah thats what i thought too. Commented Feb 25, 2014 at 10:24

2 Answers 2

1

When running instruments on your method, I got the following results:

enter image description here

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. It will be enough to set that only once!
  • all other calls are almost negligible
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for your answer. Could you show me how to fix it?
As cyrille said, make a property for your context and reuse it (just as you do with your drawImage image view). And I´m not entirely sure why you need to call the "drawImage" method at all. That should happen automatically when you set the drawImage´s image. Also rethink whether you really have to add the drawImage to your viewHierarchy every time. Setting the image on it is enough for your UI to display the image!
1

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/

The iOS App Performance: Graphics and Animations video

2 Comments

Thanks, though my codes is pretty different to the demo example.
Perhaps you can take some inspiration from the methods used by the Apple performance engineers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.