Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Best way for Lazy loading images in iPhone?

I want to know, what is the best way for lazy loading. For me, most of the applications i have used parsing and get the url from the server and put the image into the table view. So i have implemented lazy loading for improving the performance of the application. Now i want to know the best method for lazy loading images. Because i have used lazy loading into the four ways,

  • Lazy loading images from Apple developer.com

  • Implemented Asynchronous method for improving the lazy loading

  • Used separate main thread for handling the image downloaded.

  • Have used ECOImageLoadingDemo application for lazy loading.

But i have used the above four methods to achieve the lazy loading. But i want to know what's the best method for lazy loading. Which one is best for performance wise and memory wise is suitable for that?

Thanks in Advance.

Regards,

Pugal

like image 507
Pugalmuni Avatar asked Feb 18 '11 10:02

Pugalmuni


2 Answers

From my experience, performance-wise and memory-wise solutions are on the opposite ends of a slider. You can move around with your solution somewhere between these two, but with the disadvantage that having the best solution performance-wise, usually means a worse solution memory-wise and vice-versa. I hope I explained this clear enough :)

Here's how I handle the problem of lazy loading images:

In my application I created ONE entity which I called GlobalImageProvider. All requests for images go through this entity. This way I have control on how many threads I use to download and I can implement a caching system (memory + local disk), all of these completely transparent to the application and with full control. By controlling the size of the cache, I can control how quick the application feels. Performance wise, nothing compares with having an UIImage already created in memory. Memory-wise, you can even choose to disable the cache.

Even more, I can even change the number of threads dynamically while the application is running depending on the quality of the network I have.

To make the online requests, I'm using NSURLConnection but I plan on switching to something else since I've read that it leaks memory.


On the view&controllers side, I have a AsyncImageView which is just a UIImageView that knows how to work with the GlobalImageProvider. It knows to display an activity indicator while loading and can handle the response from GlobalImageProvider.
If you know the URL of the image you want, all you need to do is add a AsyncImageView to your screen and make a request to the GlobalImageProvider with the AsyncImageView as the "handler" for that image.

If you don't like mixing data with the image views, you can add a ViewController between the GlobalImageProvider and AsyncImageView. He gets the image response and puts it in the ImageView.

That's about it, hope it helps you a bit.

like image 147
Andrei Stanescu Avatar answered Oct 30 '22 09:10

Andrei Stanescu


You can take a look at this excellent tutorial from Peepcode in the section Bonus: KVO for Cell Images

http://peepcode.com/products/iphone-view-controllers-part-ii

like image 45
Matteo Alessani Avatar answered Oct 30 '22 09:10

Matteo Alessani