views:

96

answers:

1

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?

+2  A: 

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
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?
Sunsu
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
Thanks again for your help! People like you make StackOverflow awesome!
Sunsu