0

This is somewhat of a hypothetical question, but I can imagine this situation coming up at some point in the future: Let's assume for a moment that I have a freakishly complicated hierarchy of UIViews that I'd need to render only once. Let's also assume that, once drawn, I have no further use for the information (UIImages, labels, custom views, coords, etc.) beneath the parent view. Rather than retaining them, the idea is to free all the extra memory they use while avoiding a redraw. The result would be the same as drawing to an offscreen buffer and then pushing it onscreen. Is it possible to achieve this using UIView/CGLayer right out of the box, or is the only option to convert the content of the parent CGLayer into a UIImage?

Just curious. I'd imagine in most situations the overhead of keeping a few extra views around is negligible, but memory is memory, and I haven't been able to find anything on it in the official docs beyond allocating bitmaps.

Thanks!

David R.
  • 268
  • 4
  • 6

2 Answers2

0

I can think about overriding -(void)drawRect:(CGRect)rect; method, drawing only once to offscreen before moving that image to the screen according to some boolean flag, but it may lead to a monstrous bugs and should be desined properly. Another thing is that it may lead to overriding also the -(void)layoutSubviews;, thought it may be overhead if you've overriden the firs one...

Ariel
  • 2,430
  • 1
  • 17
  • 20
0

This strikes me as something very risky. Things beyond your control may happened that require the view to be redrawn, such as a notification appearing or the view being effected by the phone call active green bar thingy at the top of the screen.

I suggest that if you have that many views in your hierarchy there will be so many other performance problems that come into play that saving a few bytes after redraw will be the least of your problems.

Jack Cox
  • 3,290
  • 24
  • 25
  • Thanks, that's pretty much what I'd landed on in terms of actually developing things, but from a purely hypothetical POV I was still curious if such a solution was even possible. Also, sorry about the late response-- it would seem the notification e-mails from these boards are being eaten somewhere. – David R. Sep 21 '11 at 23:33
  • Frankly, I don't know of a way to do this other than taking 'screenshot' of the resulting view and replacing the entire hierarchy with a single image view. It would be easy to release all of the data backing up the views and then retrieve that data if the view needed to get redrawn. Like Ariel said below, overriding drawRect could easily lead to freakish defects. – Jack Cox Sep 22 '11 at 10:27