In a view controller I have a UIImageView as a subview. On willRotateToInterfaceOrientation, i replace it with a different UIImageView. If both are in memory at the same time, it sometimes jettison crashes because the images are very big. So i want to make sure I completely dealloc the first one before making the new one. When i call removeFromSuperview on it, I think it is basically autoreleasing at some point later, but I need it to be dealloced immediately.
So it seems i need my own autorelease pool when the UIImageView is created, and then drain it when willRotateToInterfaceOrientation gets called, after the removeFromSuperview call. But the documentation says:
An autorelease pool should always be drained in the same context (such
as the invocation of a method or function, or the body of a loop) in
which it was created. Autorelease pools are used “inline.” There
should typically be no reason why you should make an autorelease pool
an instance variable of an object.
So what is the “right” way to do this?
The best thing to do is optimize your images. It is imperative that an autorelease pool is allocated and drained within the same context. First thing you should is try to reduce the size of the image. If they are png’s try
pngcrush. If the images are still to large consider usingmmapto load portions of the image at a time.Performance Tuning