fancy fancy - 1 month ago 7
iOS Question

iOS: Performance of reloading UIImage(name:...)

I have a tableview and each row has an icon.
90% of the rows have the same icon.

In the cellForRowAtIndexPath method I load the image from an assets folder like this:

var cellImage = UIImage(named: "MyImage")


Does this pose a performance problem? Is the OS somehow smart enough to now that it's the same image each time? Would it be better if I loaded the image once and saved it in a class variable and assigned it each time?

Answer

UIImage's named: API actually DOES do some kind of caching.

If you look at UIImage's documentation, it states:

Use the init(named:in:compatibleWith:) method (or the init(named:) method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently.