views:

335

answers:

1

I want to be able to add one allocation of a UIImageView to multiple UIViews. How would I do this without allocating multiple UIImageViews?

+2  A: 

You can't have a view appear in more than one place. If you try then it will just get moved. Note, though, that UIImages are cached and should be handled efficiently when referred to by multiple UIImageViews, so I wouldn't worry too much about multiple views.

If these multiple views are not displayed at the same time, you could just keep moving the UIImageView around in viewDidAppear.

Nimrod
Well I want the UIImageView to show in two ImageViews at the same time, would it be poor memory management to allocate multiple UIImageViews?
Jaba
I'm not sure how bad this actually is in memory terms if both views are pointing to the same UIImage. Anyone else know?
Nimrod
First of all, as Nimrod said, a UIView can have only one superview, that is you cannot add a view to multiple superviews. Second, a UIView is a very light-weight object, so having one more UIView instance is not a problem. Third, assign the same UIImage instance to your UIImageViews to avoid copying image data. That will reduce your memory usage to the minimum.
Costique
BTW, if you use `[UIImage imageNamed:...]` to load the image then it will cache the image on load and subsequent calls with the same name will return the same pointer.
Nimrod
Costique: I disagree with your assertion that `UIView`s are lightweight. Compared to `CALayer`s they're heavy, and compared to drawing textures in OpenGL they're heavier still.
rpetrich