views:

29

answers:

1

I have a UIScrollView with a custom content view that uses drawRect to display its contents.

In my drawRect method I respect the passed CGRect parameter and only draw what's needed.

However, each time the drawRect is called it is passed the entire bounds of the view, even if it is several thousand pixels, and I'm worried about performance and memory use. Is there any way to make a UIScrollView limit this, or am I worrying too much over nothing?

Also, I've tried using CATiledLayer as the layer, and while this corrects the passed rect, it feels like I'm misusing the class. Also, the view still keeps all the pixels backed as far as I can tell. (Even if it doesn't draw some of them right away)

+1  A: 

using CATiledLayer is probably the best option. I'm familiar with CATiledLayer as a concept, but never used it myself, so I'm not going to say anything about that. What I can say, is that if you subclass the UIScrollView and implement

layoutSubviews

you should be able to redraw the subviews toll-free with good performance, as long as you implement a construction using

CGRectIntersectsRect()

In which you can see if the current visible rect, in the case of a subclass

[self bounds]

intersects with the rect of the object you want to draw. if it doesn't, you can choose to ignore the object or even (if it is already on the scrollview) remove it from superview. if it does, you can place the object on superview.

again, CATiledLayer is a way better solution, but if you feel like you are misusing it, try the approach I described.

MiRAGe
I think I can make this work. I need to break the large view up into smaller views, then have some method of reusing them (like UITableView does). The reason I didn't want to use CATiledLayer is that it automatically includes drawing in a background thread, which wasn't what I wanted.
cobbal