I want to be able to add one allocation of a UIImageView to multiple UIViews. How would I do this without allocating multiple UIImageViews?
views:
335answers:
1
+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
2010-01-14 18:32:40
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
2010-01-14 19:49:26
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
2010-01-15 03:07:13
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
2010-01-15 09:42:15
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
2010-01-18 01:25:24
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
2010-06-03 20:21:54