Search code examples
iphonecore-graphicsdrawrectlag

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 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!!

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)


Solution

  • 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.