Difference between [UIImage imageNamed…] and [UIImage imageWithData…]?

It depends on what you’re doing with the image. The imageNamed: method does cache the image, but in many cases that’s going to help with memory use. For example, if you load an image 10 times to display along with some text in a table view, UIImage will only keep a single representation of that image in memory instead of allocating 10 separate objects. On the other hand, if you have a very large image and you’re not re-using it, you might want to load the image from a data object to make sure it’s removed from memory when you’re done.

If you don’t have any huge images, I wouldn’t worry about it. Unless you see a problem (and kudos for checking Object Allocation instead of preemptively optimizing), I would choose less lines of code over negligible memory improvements.

Leave a Comment