下载图片时的进度条
我将如何添加进度条来下载大图像或任何与此相关的文件?这是我的代码:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *downloadPath = [[[NSString alloc]initWithFormat:@"http://www.zzz.com/%@.jpg",chartFileName]autorelease];
NSString *savePath = [[[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath, chartFileName]autorelease];
NSURL *url = [[NSURL alloc]initWithString:downloadPath];
NSData *downloadedChartData = [NSData dataWithContentsOfURL:url];
[url release];
[downloadedChartData writeToFile:savePath atomically:YES];
How would I go about adding a progress bar for downloading a large image, or any file for that matter? here is my code:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *downloadPath = [[[NSString alloc]initWithFormat:@"http://www.zzz.com/%@.jpg",chartFileName]autorelease];
NSString *savePath = [[[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath, chartFileName]autorelease];
NSURL *url = [[NSURL alloc]initWithString:downloadPath];
NSData *downloadedChartData = [NSData dataWithContentsOfURL:url];
[url release];
[downloadedChartData writeToFile:savePath atomically:YES];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 NSURLConnection 调用中覆盖 connection:didReceiveData: 。请参阅此处了解更多信息:
如何将 NSURLConnection 与 UIProgressView 集成?
You could override connection:didReceiveData: in your NSURLConnection call. See here for more:
How to integrate NSURLConnection with UIProgressView?
在后台运行选择器来检查图像的下载大小,并将其与所需的大小进行比较。然后将进度条的百分比设置为已下载/需要。
Run a selector in the background that checks the image's download size, and compares it against the required size. Then set the % of the progress bar to downloaded/required.