NSURLConnection下载文件时,如何显示进度
2012-10-26 16:28
465 查看
转自: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_);
}
}
有了要下载的数据的总大小,和已经下载的大小,进度自然就出来了。
这里以下载图片举例。
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下载文件时,如何显示进度
- 用idhttp打开网页或下载文件时如何显示进度
- android中如何下载文件并显示下载进度
- NSURLConnection下载文件并显示进度(HEAD)
- Android中如何下载文件并显示下载进度
- NSURLConnection下载文件并显示进度(HEAD)
- NSURLConnection下载文件并显示进度(HEAD)
- NSURLConnection下载文件并显示进度
- android中如何下载文件并显示下载进度
- MAC 10.8系统Mountain Lion,一些从网上下载的.dmg文件,打不开,显示“打不开…,因为它来自身份不明的开发者”如何解决?
- 下载文件并显示进度条
- 实现文件下载并且显示下载进度
- 基于XUtil工具的多个文件下载显示总进度
- python学习--下载--下载固定文件显示下载百分之的进度
- C#如何调用Windows系统自身的文件复制功能,显示文件进度,显示重名提示
- libcurl 通过http协议下载文件并显示下载进度
- 下载远程文件, 加入一个进度显示 分类: python 2013-06-09 12:09 314人阅读 评论(0) 收藏
- libcurl开源库在Win32程序中使用下载文件显示进度条实例
- VC下载文件 + 显示进度条
- Android 下载文件 显示进度条