How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue

一曲冷凌霜 提交于 2019-12-29 03:12:51

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/3380791/how-uitableviewcell-with-uiimageview-asynchronously-loaded-via-asinetworkqueue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!