I use UIBezierPath for finger painting (my app). I create it using path = [UIBezierPath bezier path]; . It constantly lags on the iPad (and calling it from within drawrect did not change anything). I have been working on this for hours on end and have found no solution, just lag. Would somebody be so kind to help me please? Also, I am using a NSTimer to call the function. That is the old way my app will work so please help me fix this lagggg!!!!!
Share
Since none of your questions contains enough details on its own, I’m going to do something improper and post a meta-answer to your current last five questions.
First, draw in
drawRect:and nowhere else.That’s so important that I’m going to say it again.
Draw in
drawRect:and nowhere else.Not
touchesMoved:withEvent:.Not
touchesBegan:orEnded:.drawRect:and nowhere else.If you’re making an image to save to a file, that’s one thing. But when you’re drawing to the screen, you don’t do it anywhere other than
drawRect:.Seriously. It’s that important.
Step 2: Don’t attempt to force drawing to happen at any other time.
drawRect:is called for you when it’s time for you to draw. By definition, at any other time, you don’t need to draw, so drawing is doing things you don’t need to do. By extension, don’t calldrawRect:yourself. You don’t need to and it never helps.Actually, that’s just an extension of step 1. If you call
drawRect:, it’s no different from if you had the drawing code where you have the call. The drawing code isn’t repeated everywhere, which is nice, but it’s still running at the wrong time. LetdrawRect:only be called when the system calls it.setNeedsDisplayexists to tell the system that it’s time to draw. You should do this when, and only when, something has changed that you’ll need to draw. Your view’s properties, something in the model—whenever what you will draw changes, send yourselfsetNeedsDisplay. Don’t do it at any other time; you don’t need to.Cut out the timer. You don’t need it. There’s already a timer in place anyway, limiting you to 60 fps.
Core Graphics does not lag. No, really, it doesn’t. Slowness or “lag” is because either you’re trying to do too much or you’re doing something wrong.
Don’t cache unnecessarily. Nothing you’re doing requires an image cache.
The “lag” is because you’re trying to draw, or to force drawing, from
touchesMoved:withEvent:and/ortouchesBegan:/Ended:. See above.Here’s what you need to do:
In your
touchesBegan:/Moved:/Ended:methods, or other responder methods as appropriate, update your state. This will include the Bézier path. Do not draw, and that includes do not calldrawRect:or otherwise attempt to force drawing.After you’ve updated your state, and only if you’ve done so, send yourself
setNeedsDisplay.In your
drawRect:method, and only in yourdrawRect:method, draw the path, gradient, whatever, however you see fit.Do these things, and your application will be fast. With Core Graphics. Without lag.
Also, some important reading: