开发者

Any possible way for CoreGraphics to not lag when drawrect is being called from touches moved?

I am calling setNeedsDisplay from touches moved (and have also tried not calling from touches moved, but instead from a 0.05 timer) and the drawrect method is always laggy. Is their anyway to change this? I am doing a lot of drawing in drawrect b开发者_开发知识库ut I have no idea for a solution to fix the lag. Even when the timer was called at a 1.0 interval than it still lagged when the timer called the selector. Also, I have no leaks (I checked using Xcode analyze feature ). Please help!!

EDIT: I am calling setNeedsDisplay, not drawRect from my timer/method

EDIT: It seems that wherever core graphics does somethings with a lot of drawing it always lags. I am positive I have no memory leaks and I even created another painting app and it lags (what is the fix to this?? Please help mee)


Slightly edited transcript of comments on one of the other answers:

I am drawing a color a hue based color picker (in draw rect a line for each hue value is drawn)

… Are you drawing 360 rectangles?

Yes, I am ….

I draw the images of 360 rectangles of different colors into the image of one UIImageView. than I release the rectangles. (I use a for loop for the rectangle allocation/releasing)

So, you are doing this 360 times:

  1. Create an image.
  2. Draw a rectangle into this image. (Or not, if step 1 loads the image from a file.)
  3. Draw that image into another image.
  4. Release the image.

And then you pass the image that you drew all the smaller images into to a UIImageView for the actual display?

It sounds like you're trying to cache this in the image, which should help after the first time if you do it right, but this doesn't need to be slow in the first place.

You already have a custom view. Drop the image view, and cut out all this rectangle-drawing (or image-drawing) code. If you have image files with the individual colored rectangles, delete them.

In your view's drawRect:, create a gradient and draw that. Your drawRect: will be three lines long and should be much, much, much faster.


I am calling drawrect from touches moved

don't do that.

(and have also tried not calling from touches moved, but instead from a 0.05 timer)

don't do that.

and the drawrect method is always laggy. Is their anyway to change this? I am doing a lot of drawing in drawrect but I have no idea for a solution to fix the lag. Even when the timer was called at a 1.0 interval than it still lagged when the timer called the selector. Also, I have no leaks (I checked using Xcode analyze feature ). Please help!!

Yes!


You should NEVER call drawRect explicitly. Use setNeedsDisplay instead and the drawing will be performed when the system is ready for it.

EDIT: Based on the fact that you were already doing this. Your problem is then your drawRect is just too slow. What are you trying to draw?


If you can figure out which parts of the screen needs change, you can call setNeedsDisplayInRect to speed it up by just redrawing the changed rect instead of the whole screen.

You can also run a background thread to prepare frames in a buffer and use that to draw on screen. It depends on the kind of drawing you are doing. Here is a blog post I found on this topic.

Or you can use OpenGL ES to draw.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜