Reputation: 81
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,
Upvotes: 0
Views: 753
Reputation: 726539
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.
Upvotes: 1
Reputation: 130102
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.
Upvotes: 0
Reputation: 45598
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.
Upvotes: 0