Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue

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?

like image 996
Lyle Pratt Avatar asked Aug 01 '10 04:08

Lyle Pratt


1 Answers

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
like image 94
JosephH Avatar answered Oct 27 '22 07:10

JosephH