2

I am using ASIHTTPRequest. I have the following issues while using ASIHTTPRequest.

1.) I need to add images to UITableView (for each cell) asynchronously. How can i do this ?

2.) I need to add an image to a UIViewController Asynchronously. (Not to a cell, but just on the UIImageView, which is on a UIViewController).

Can someone please help me with some sample code, Example or a Tutorial to start with?

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Illep
  • 16,375
  • 46
  • 171
  • 302
  • possible duplicate of [How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue](http://stackoverflow.com/questions/3380791/how-uitableviewcell-with-uiimageview-asynchronously-loaded-via-asinetworkqueue) – JosephH Jan 28 '12 at 19:37

3 Answers3

6

No need to introduce a dependency to a whole framework such as ASIHTTPRequest just to download one image, when you can do it a few easy lines of code using GCD:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *imageDate = [NSData dataWithContentsOfURL:imageURL];
    UIImage *image = [UIImage imageWithData:imageData];
    dispatch_async(dispatch_get_main_queue(), ^{
        avatar.image = image;
    });
});

This is asynchronous and all the goodness. But in a few lines of code you can write, understand, bug-fix, extend and maintain yourself.

But in case you are bent on using ASIHTTPRequest I suggest using this excellent project Here is a sample code to have a guide line and a brief description.

One other way is that you can use the asynchronous image view instead of the default image view. check tutorial Here and also How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • Hey, is it OK to use multiple libraries in a project ? As in async-uitableview and ASIHTTPRequest ? and what is so special about using another library when you can do it in only ASIHTTPRequest ? (I am a beginner) and also the brief description link ends up in a 404, could you kindly re-link it – Illep Jan 28 '12 at 16:05
  • yes you can use multiple libraries in same project. `async-uitableview` is an abstraction on top of `ASIHTTPRequest. `async-uitableview` specifically addresses asynchronous download of images in tableview. – Srikar Appalaraju Jan 28 '12 at 16:22
  • @Srikar dispatch_async - I saw it first time today and used it. Thanks for this info. It looks good method to use. – pankaj Apr 25 '12 at 19:09
0

Basically (see example page with all the correct syntax) what you do is

  • Create a request.
  • Set yourself as a delegate for when it's complete.
  • Start the request.

In -(void)requestFinished: you add the image to your TableView as you would in normal code.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • I am already using `requestFinished` to get other data to be displayed in the tableview. and this data that i will be receiving will have a link to the image i wish to download. So how can i use `requestFinished` again to download the image ? – Illep Jan 28 '12 at 16:21
  • When requestFinished is called, the file is already downloaded, and you just use NSData *responseData = [request responseData]; to get to the actual downloaded content. – Joachim Isaksson Jan 28 '12 at 16:24
0

I'm not sure that it's exactly what you are looking for, but check out the lazy image loading example from apple. But they use NSURLConnection.

Hope it helps

Novarg
  • 7,390
  • 3
  • 38
  • 74