9

I'm trying to load some images in table cells asynchronously using ASINetworkQueue. I just can't figure it out and can't seem to find a good SIMPLE example.

The best I can find is this, but its just totally overkill and a little too complicated for me: http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/

Does anyone else have any tips/solutions/code for doing this with the ASIHTTPRequest library?

DrummerB
  • 39,814
  • 12
  • 105
  • 142
Lyle Pratt
  • 5,636
  • 4
  • 27
  • 28

1 Answers1

20

Here's a class derived from UIImageView which I use, perhaps this will help you. (Actually I've simplified this a fair bit from what I use, but that was what you asked for!)

Header file, UIHTTPImageView.h:

#import "ASIHTTPRequest.h"

@interface UIHTTPImageView : UIImageView {
    ASIHTTPRequest *request;
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;

@end

and UIHTTPImageView.m:

#import "UIHTTPImageView.h"

@implementation UIHTTPImageView        

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
    [request setDelegate:nil];
    [request cancel];
    [request release];

    request = [[ASIHTTPRequest requestWithURL:url] retain];
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];

    if (placeholder)
        self.image = placeholder;

    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)dealloc {
    [request setDelegate:nil];
    [request cancel];
    [request release];
    [super dealloc];
}

- (void)requestFinished:(ASIHTTPRequest *)req
{

    if (request.responseStatusCode != 200)
        return;

    self.image = [UIImage imageWithData:request.responseData];
}

@end
JosephH
  • 37,173
  • 19
  • 130
  • 154
  • This seems like exactly what i'm looking for. I do have one question though: Since it is not using a "network queue" if the number of rows in the table is really big, won't it overload? – Lyle Pratt Aug 01 '10 at 18:47
  • 1
    It's using ASIHTTPRequest's shared queue, which by default will do 8 concurrent downloads at most. You can reduce this with something like [[ASIHTTPRequest sharedQueue] setMaxConcurrentOperations:2] if you want. (Note: needs latest version of asihttprequest from git, v1.7 and earlier don't expose the sharedqueue.) – JosephH Aug 02 '10 at 07:23
  • 2
    Thanks again for your help! People like you make StackOverflow awesome! – Lyle Pratt Aug 03 '10 at 00:22
  • i know so m so dumb. how to use it with my UIimageView – carbonr May 11 '12 at 16:46
  • 1
    @carbonr Replace your use of UIImageView with UIHTTPImageView, then just use [img setImageWithURL:url placeholder:nil]; – JosephH May 11 '12 at 17:43