我使用下面的代码异步地从url下载文件,
NSMutableData *responseData = [[NSMutableData alloc] init];
NSURL *url = [NSURL URLWithString:@"http://www.tuiscos.com/images/trading.png"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// do something with data
[responseData appendData:data];
myImage8.image = [UIImage imageWithData:data];
NSInteger len = response.expectedContentLength;
NSInteger receiverdBytes = 0;
receiverdBytes = data.length+ receiverdBytes;
float prog = (float)[responseData length]/(float)len;
[progress8 setProgress:prog];
}];随着下载的进行,我想更新进度条,但使用这段代码,我不会得到一个渐进的进展,相反,它是等待完成下载和跳转到最大值。我怎样才能逐步提高这个值呢?有人能提供一个示例代码吗?对于带有委托方法的异步方法.谢:)
发布于 2012-06-07 21:35:26
如果您不想自己编写所有代码,我建议在此任务中使用ASIHTTPRequesst:
http://allseeing-i.com/ASIHTTPRequest/How-to-use
它的实现非常简单,您可以同时进行异步下载。它还为所有需要提供代表,也为进度更新提供代表。
我在我的项目中使用了将近一年,从来没有后悔过。
发布于 2012-03-28 13:20:50
CompletionHandler当然是在完成时执行的。您必须为连接指定委托。使用-initWithRequest:delegate:方法。您必须编写NSURLConnectionDelegate方法的代码,需要设置progressView值的方法是-connection:didReceiveData:
https://stackoverflow.com/questions/9908157
复制相似问题