使用 ASIHttpRequest 下载多个图像
Posted
技术标签:
【中文标题】使用 ASIHttpRequest 下载多个图像【英文标题】:download multiple images with ASIHttpRequest 【发布时间】:2012-02-28 13:46:46 【问题描述】:我有一个包含多个单元格的 UITableView。当我单击一个单元格时,我会推动另一个控制器来显示该单元格的详细信息。详细来说,我有一个滚动视图,其中包含几个UIImageViews
。我想在加载详细视图时使用 ASIHTTPRequest 从 Web 下载这些视图的图像。
【问题讨论】:
How? UITableViewCell with UIImageView asynchronously loaded via ASINetworkQueue的可能重复 【参考方案1】:您可以覆盖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];
【讨论】:
这个可以同时下载多少张图片? iPad的门槛是多少?因为我有图像的网格视图(最多大约 1000 个,但我一次加载 250 个)。这对我有用吗?【参考方案2】:这是一个从 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: 方法。
【讨论】:
以上是关于使用 ASIHttpRequest 下载多个图像的主要内容,如果未能解决你的问题,请参考以下文章
ASIHttpRequest - 移动到实时 PHP 服务器后不创建临时文件