如何使用ASINetWorkQueue下载实现
假设有这么个触发下载方法叫
- (IBAction)startDownLoad:(id)sender
本地viewController有这么个instance variable : ASINetWorkQueue *netWorkQueue
可以从该实例的头文件中看到该queue属于一个NSOperationQueue
在触发方法实现如下代码
// lazy init 你的实例netWorkQueue
if (!networkQueue) { networkQueue = [[ASINetworkQueue alloc]init]; }
// 使得每一次下载都是重新来过的 [networkQueue reset]; //UI监听器,设置用来监察整个过程的UI界面 [networkQueue setDownloadProgressDelegate:wholeProgress]; // [networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
// 设置queue完成后需要实现的UI方法,根据头文件里面定义,这个UI方法需要一个ASIHTTPRequest 的参数 networkQueue.requestDidFinishSelector = @selector(imageFetchComplete:); [networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)]; networkQueue.showAccurateProgress = [self.accurateInfo isOn];
// 如果要实现SEL的方法则根据头文件定义需要把delegate定为self networkQueue.delegate = self;
//这里是给queque添加任务的。 ASIHTTPRequest *request;
//定义request request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];
//定义request的下载目标地址 request.downloadDestinationPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"1.png"];
//定义request 下载的ui同步 request.downloadProgressDelegate = self.imageView1Progress;
//纪录request信息 request.userInfo = [NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]; [networkQueue addOperation:request];
- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
}