0

Iim displaying a huge amount of images each time I call drawRect with setNeedsDisplay. The problem is that cause lags cause each time I call dracRect, I redraw every images (thousands).

Is there a way to not redraw all the images. I mean, keep all images and draw the one I want to draw?

thanks,

3 Answers3

1

One way to avoid redrawing everything is to call setNeedsDisplayInRect: instead of setNeedsDisplay:, and passing only a rectangle containing the image that needs to be redrawn. Of course this means that your drawRect can no longer ignore the rectangle passed in: go through the images, and check if an image overlaps with the rectangle being redrawn. If there is no overlap, skip the image and save some CPU time on a redraw.

Note that this is a very "manual" way of maintaining a view. Consider using some of the components that iOS provides for you, such as UICollectionView, which lets you display lots of stuff on the screen with very little code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @DavidGoncalves Could you be more specific about the result that you get vs. the result that you want then? – Sergey Kalinichenko Apr 12 '13 at 14:20
  • Yeah, I did It, but the result is not the result I want. I want this result : http://imageshack.us/photo/my-images/826/afterf.png/. my cards have transparency. but I have this result : http://imageshack.us/photo/my-images/40/beforevy.png/. Why? – David Goncalves Apr 12 '13 at 14:24
  • @DavidGoncalves There is little similarity between the two pictures: it looks like everything has been wiped out before drawing the second one. You need to check that the call of `setNeedsDisplayInRect:` passes the correct rectangle (i.e. one containing the card that has moved) and that your `drawRect:` checks the incoming rectangle before drawing the required images. – Sergey Kalinichenko Apr 12 '13 at 14:29
  • Yes setNeedsDisplay: passes the correct Rect. The screen you see around the cards in the second screenshot is normally transparent. In the first screen, the transparency is ok. The card that I draw is good, the transparency, to, but the cards drawn before, all have no more transparency. I don't know why. – David Goncalves Apr 12 '13 at 14:37
0

You will have to split your content into multiple views or layers (CALayer). Then only redraw the view or layer whose content actually needs to change.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
0

Split the view into different views (or layers), that will enable the system to cache their contents and redraw faster.

I don't know your use case but with hundreds of images, I would consider using OpenGL.

Sulthan
  • 128,090
  • 22
  • 218
  • 270