使用ASIHttpRequest下载多个图像

时间:2012-02-28 13:46:47

标签: objective-c ios asihttprequest

我有一个包含多个单元格的UITableView。当我单击一个单元格时,我按下另一个显示该单元格细节的控制器。在细节中,我有一个包含多个UIImageViews的scrollView。我想在加载详细信息视图时使用ASIHTTPRequest从Web下载这些视图的图像。

2 个答案:

答案 0 :(得分:4)

您可以覆盖UIView。每个视图都有一个ASIHTTPRequest。每次下载完成后,您可以使用drawRect绘制下载的图像。

这是一个演示:

MyImageView.h

#import <UIKit/UIKit.h>

#import "ASIHTTPRequest.h"

@interface MyImageView : UIView <ASIHTTPRequestDelegate>
{
    ASIHTTPRequest *httpRequest;
    UIImage *image;
}

- (void)startRequest:(NSString *)_url;
@end

MyImageView.m

#import "MyImageView.h"

@implementation MyImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    // Drawing code
    if (image != nil) {
        [image drawInRect:self.bounds];
    }

}

- (void)dealloc
{
    [httpRequest clearDelegatesAndCancel];
    [httpRequest release];
    [image release];

    [super dealloc];
}


-(void)startRequest:(NSString *)_url
{
    if (httpRequest != nil) {
        [httpRequest clearDelegatesAndCancel];
        [httpRequest release];
    }

    httpRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:_url]];
    [httpRequest setTimeOutSeconds:30];

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

- (void)requestFinished:(ASIHTTPRequest *)request
{
    if ([request responseStatusCode] == 200) {
        image = [[UIImage alloc] initWithData:[request responseData]];
        [self setNeedsDisplay];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"request failed");
}

用法:

MyImageView *imageView = [[MyImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
[imageView startRequest:@"http://imageUrl"];
[self.view addSubview:imageView];
[imageView release];

答案 1 :(得分:1)

这是一个派生自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

请注意,没有错误报告,如果要向用户报告问题,可能需要requestFailed:方法。

相关问题