Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy load images in UITableViewCell

I have some 50 custom cells in my UITableView. I want to display an image and a label in the cells where I get the images from URLs.

I want to do a lazy load of images so the UI does not freeze up while the images are being loaded. I tried getting the images in separate threads but I have to load each image every time a cell becomes visible again (Otherwise reuse of cells shows old images)

Apps like Facebook load images only for cells currently visible and once the images are loaded, they are not loaded again. Can someone please tell me how to duplicate this behavior.

Thanks.

Edit
Trying to cache images in an NSMutableDictionary object creates problems when the user scrolls fast. I am getting images only when scrolling completely stops and clearing out the cache on memory warning. But the app invariably gets a memory warning (due to size of images being cached) and clears the cache before reloading. If scrolling is very fast, it crashes.

Any other suggestions are welcome

like image 457
lostInTransit Avatar asked Feb 10 '09 08:02

lostInTransit


People also ask

What are lazy loading images?

Lazy Loading Images is a set of techniques in web and application development that defer the loading of images on a page to a later point in time - when those images are actually needed, instead of loading them up front.

Can I use lazy loading images?

In Chrome 76 onwards, you can use the loading attribute to lazy-load images without the need to write custom lazy-loading code or use a separate JavaScript library. Let's dive into the details.

What is lazy loading in IOS Swift?

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used. The opposite of lazy loading is eager loading.

Does lazy loading images affect SEO?

Lazy loading images improves the user experience by saving bandwidth for important content first. Some reject the technique for SEO considerations. But properly lazy loading your images does not prevent them from being indexed.


1 Answers

Loading the images on a background thread is still a good idea. If you didn't want to reload them each time, I'd suggest setting up an NSMutableDictionary and storing the images in there. You could use some unique identifier, like the row ID or even the name of the image, as the key for each image.

When loading a cell, you'd send an objectForKey: message to the NSMutableDictionary to retrieve the image for that particular cell (based on your unique key for it). If it returns nil, that means that the image is missing from the cache and you need your background image loading thread to go retrieve it. Otherwise, you will get back the appropriate image for your table cell to display. On a memory warning, you could clear out this cache of images with no adverse effects (aside from forcing them to be reloaded again on demand).

like image 75
Brad Larson Avatar answered Oct 05 '22 12:10

Brad Larson