NSURLConnection下载文件时,如何显示进度
来源:互联网 发布:星际争霸重制版 mac 编辑:程序博客网 时间:2024/05/16 05:33
转自:http://iliunian.diandian.com/post/2011-12-27/17217880
这里以下载图片举例。
1,首先创建一个connection
downloadImage是一个线程函数,在子线程中下载图片。
//url 图片的url地址
- (void) downloadImage:(NSString*)url{
self.uploadPool = [[NSAutoreleasePool alloc] init];
self.characterBuffer = [NSMutableData data];
done = NO;
[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
self.connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[self performSelectorOnMainThread:@selector(httpConnectStart) withObject:nil waitUntilDone:NO];
if (connection != nil) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!done);
}
self.photo = [UIImage imageWithData:characterBuffer];
[self performSelectorOnMainThread:@selector(fillPhoto) withObject:nil waitUntilDone:NO];
// Release resources used only in this thread.
self.connection = nil;
[uploadPool release];
self.uploadPool = nil;
}
2, 在NSURLConnection的delegate中接收数据
// Forward errors to the delegate.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
done = YES;
[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
//NSLog(@"%@",[error localizedDescription]);
[characterBuffer setLength:0];
}
// Called when a chunk of data has been downloaded.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Process the downloaded chunk of data.
//NSLog(@"%d", [data length]);
received_ += [data length];
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
[characterBuffer appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSLog(@"%lld", received_);
[self performSelectorOnMainThread:@selector(httpConnectEnd) withObject:nil waitUntilDone:NO];
// Set the condition which ends the run loop.
done = YES;
}
上面三个方法,第一个是连接出错的情况,第二个是接收数据,reveived_纪录一共接收了多少子节的数据。第三个是连接结束。
还有一个方法,我们可以从http返回的response中拿到一些连接的信息。这里我拿到了要下载的数据的大小,保存在"Content-Length"的字段中,详细的理论见HTTP协议。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if(httpResponse && [httpResponse respondsToSelector:@selector(allHeaderFields)])
{
NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];
total_ = [[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue];
//NSLog(@"%lld", total_);
}
}
有了要下载的数据的总大小,和已经下载的大小,进度自然就出来了。
- NSURLConnection下载文件时,如何显示进度
- NSURLConnection下载文件时,如何显示进度
- NSURLConnection下载文件并显示进度
- NSURLConnection下载文件并显示进度(HEAD)
- NSURLConnection下载文件并显示进度(HEAD)
- NSURLConnection下载显示进度
- android中如何下载文件并显示下载进度
- android中如何下载文件并显示下载进度
- Retrofit文件下载进度显示
- 安卓文件下载,显示下载进度
- NSUrlConnection 下载文件
- 通过NSURLConnection下载文件
- NSURLConnection下载大文件
- WP7-文件下载进度和速度显示
- android AsyncTask下载文件并且显示进度
- 下载远程文件, 加入一个进度显示
- 阿里云 oss 文件下载进度显示
- 解决Retrofit文件下载进度显示问题
- 程序员定律(四大定律)
- [Android] 通过代码调用返回键
- 你真的愿意到了50岁还要做编程吗?
- C++函数中哪些不可以被声明为虚函数
- C#文件下载四方法
- NSURLConnection下载文件时,如何显示进度
- 防止程序崩溃时错误提示对话框弹出
- MySQL connector/C++ 连接mysql效率低下解决
- 【十】WinXP+IIS6 配置Discuz和phpwind论坛全完手册---附录1
- 利用XRDP远程登陆linux系统
- 第九周作业
- 数据结构之堆
- POCO库中文编程参考指南(10)如何使用TCPServer框架?
- 编程思维训练(一)