Let’s say I have a UIImage 1000×1000 and I have a UIImageView with a frame size of 50×50. If I set my UIImageView’s image as the large image, is the UIImage automatically resized to 50×50? The UIImageView contentMode is ScaleToFit.
I know that the image does shrink down to 50×50 to fit in there, but in terms of memory, would it be better that I resized the image to 50×50 before setting it as the UIImageView image?
I know it won’t make a difference for one image, but I have hundreds of large images and image views in a UIScrollView, so I want to make sure I get the fastest and smoothest performance.
I can think of a couple considerations.
UIImage ImageNamed:will cache the image for the entire app run, even when no longer used. If arbitrary images will be used or added as opposed to a static set, make sure to load the image with NSData from a connection, file, or database. Also, I haven’t checked in instruments for at least several iOS versions, but I believe that the original image is cached for scaling. If there is no specific issue that you actually run into with running out of memory or slowness, don’t bother with pre-emptive optimization. Otherwise, there are some techniques that may help. First, you may only want to retain what is currently on screen. This is easier to do with a UITableView and cell reuse than with a bare UIScrollView. Second, for one app I used that held a lot of thumbnails, I scaled them once and stored them in the database.