怎么样?带有UIImageView的UITableViewCell通过ASINetworkQueue异步加载

时间:2010-08-01 04:55:57

标签: asynchronous uitableview uiimageview asihttprequest

我正在尝试使用ASINetworkQueue异步加载表格单元格中的一些图像。我只是无法搞清楚,似乎无法找到一个很好的SIMPLE例子。

我能找到的最好的就是这个,但它对我来说完全矫枉过正,有点太复杂了: http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/

其他人是否有任何使用ASIHTTPRequest库的提示/解决方案/代码?

1 个答案:

答案 0 :(得分:20)

这是从我使用的UIImageView派生的类,也许这会对你有所帮助。 (实际上我已经从我使用的内容中简化了一下,但这就是你要求的!)

头文件,UIHTTPImageView.h:

#import "ASIHTTPRequest.h"

@interface UIHTTPImageView : UIImageView {
    ASIHTTPRequest *request;
}

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

@end

和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