Show images asynchronouly in UITableView from server in iPhone - iphone

When I try to fetch the images in UITableView using NSURLUIImage image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://mysite.com/mobile/abc.jpg]]];
my application get freezes until the images loaded. Also when I scroll the table it get images from the server again and scroll get stuck for some time.
I know this is happening because of downloading images synchronously from server.
Is there any solution so that I can download the image asynchronously from server and store it locally or in cache so that when table scrolls it did not get images again from server.
Thanks

Check out EGOImageLoading by enormego. Works just like UIImage (and caches images) but lets you load from HTTP asynchronously.

You can create your own class which leverages NSURLConnection and it's various Asynchronous methods to load your images in the background and then have a callback to display it in the cell.
However, there are some excellent libraries which do exactly what you've described. HJCache is one i've used and recommend for this sole purpose

Related

NSData dataWithContentsOfURL slow

I have some performance problem with NSData dataWithContentsOfURL...
NSURL *url = [NSURL URLWithString:Imagepath];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img=[UIImage imageWithData:data];
[ArrayImages addObject:img];
This code is placed in a method that manage a JSON response got from an NSUrl connection(after calling my web service). All the code in this method is already in a background thread, moving this piece of code out the background thread do not solve the issue.. All the retrieved image are placed in a view in main thread. What i can do to make dataWithContentsOfURL faster or there is a alternative to dataWithContentsOfURL?
Thanks In Advance
+[NSData dataWithContentsOfURL:] is not "slow". If loading one image takes a long time, the problem lies elsewhere.
Evaluate your problem. For starters:
Which resource is the bottleneck? Probably the Network.
How do you load images? All at once? That would be bad -- display them as they become ready.
What are the sizes of the images? I saw one SO question where the poster wanted to load 50 MB images. That's way too large. As well, if all you need is a thumbnail, then be sure you request the thumbnail from the server and load that and not the full-sized image.
Are you loading things you don't even need to display? Wait until you need to display them.
How many threads are you using for Network tasks? For CPU? For I/O?
Are your source images properly "crushed"?
Write your program so it flows with your program's presentation model. Example: I had a bazillion images to display in tables, but I made sure to minimize resource usage and made sure load and request cancellation was well supported for the app. This was all coming through the network, and it was plenty fast (it was network-bound).
and if you are loading many images from device storage, you should consider using -[UIImage initWithContentsOfFile:] instead because your image data would not be cached, but can be purged.
U need to use Lazy loading for images display as content will displayed as image gets downloaded.
Use SDWebImage which uses lazy loading of images.

ios load images from web server and show on UITableViewCell

For the iPhone 5.0 (ARC) I am getting lot of image(image url's) and text data from server in a json form. My requirement is to load the first five images from the server and when the user scrolls down the next five images load and so on. I need to show it in a UITableViewCell. By doing this I can reduce the network calls and make the application faster on the device.
Currently, I am using a background thread to load images, but they continuously load in the background. I don't want to do it like this.
I strongly encourage you to use the AFNetworking framework (that is really great and complete for all network-related tasks).
It comes with a category on UIImageView that allows you to set the UIImageView's image directly from an NSURL, managing everything for you in the background, like downloading the image or fetching it from its cache, cancelling the request if you change the URL later to avoid useless requests, etc.
Then in your tableView:cellForRowAtIndexPath: you can simply write this kind of code:
[cell.imageView setImageWithURL:yourURL];
And you're done. It will work even when the recycling mechanism of the UITableView is in action when you scroll, which would be quite a pain to manage using other methods.
Take a look at SDWebImage. It's a really neat library to download images asynchronously and also caches them.

How can I speed up the loading of images from a web service?

I'm new to iPhone development. In my application, I had kept a scrollview and I have loaded the images from web service using JSON parsing. Unfortunately, it is taking too much time to load them. Do you have any suggestions on how to speed up the (down)loading?
Well there can be many routes you can take to make image loading faster but the one thing I would recommend most is utilize AFNetworking's "UIImageView+AFNetworking" category which you can find here:
http://afnetworking.com/
It loads images quickly and doesnt tie up the main thread. If you have a preloader image you want to display while it is downloading you can do that as well.
AND it's really easy:
[imageView setImageWithURL:[NSURL URLWithString:#"…"]];

How to download and load downloads image in Custom UITableViewCell?

I get the text form the sever like this:
test_abc
http://www.xxx.a.jpg
test_aaa
http://www.xxx.b.jpg
test_ccc
I want to split the string to ["test_abc", "http://www.xxx.a.jpg", "test_aaa", "http://www.xxx.b.jpg", "test_ccc"] and then display them in a UITableViewCell,before the images will be downloaded, I used a LoadingIndicator as placeholder, when the images will be downloaded, display the images and modify the height of the cell to display the whole images.
Can someone help me do this?
Try looking into Three20
An open source library written by the guy who wrote the official "Facebook" app for iOS. Moreover, the code of the facebook app is based on that library.
You may use SDWebImage, and you don't have to worry about cache, etc..
Web Image
This library provides a category for UIImageVIew with support for remote images coming from the web.
It provides:
An UIImageView category adding web image and cache management to the Cocoa Touch framework
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
A guarantee that the same URL won't be downloaded several times
A guarantee that bogus URLs won't be retried again and again
Performances!

how to download photos from remote server to iphone app?

My situation is that I am making a uitableviewcontroller with uitableviewcells having a thumb nail or picture on the left, text on the right. All data is stored in a remote server. I have made a php to communicate with backend batabase and generate JSON to pass the data to iphone through http. However, I don't know how to do the same thing to photos. Please suggest me someway to do it. Thanks!
As Mundi said it already.
And make sure that you load them asynchronously.
There is a nice tutorial on this page:
http://www.markj.net/iphone-asynchronous-table-image/
I assume you are using NSURLConnection to fetch your data from the server. Do this:
Collect the photo data into an NSData object.
In the didFinishLoading method use this to create the images:
UIImage *thumbnailImage = [[UIImage alloc] initWithData:downloadedData];
// don't forget to discard the downloaded Data and release the image
The actual setup of your cells might be more complicated. The way I do it is to check in each call to cellForRowAtIndexPath: if I have cached the image, if not I pass to URL of the image to a NSURLConnection. I keep the open connections in an array and eliminated them when each download finishes or the view disappears. Let me know if you need more hints...