【问题标题】:progress bar when downloading image图片下载进度条
【发布时间】:2010-06-24 19:10:56
【问题描述】:

我将如何添加一个进度条来下载大图像或任何文件?这是我的代码:

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];

【问题讨论】:

    标签: iphone ipad progress-bar


    【解决方案1】:

    您可以在 NSURLConnection 调用中覆盖 connection:didReceiveData:。更多信息请看这里:

    How to integrate NSURLConnection with UIProgressView?

    【讨论】:

    【解决方案2】:

    在后台运行一个选择器,检查图像的下载大小,并将其与所需大小进行比较。然后将进度条的百分比设置为已下载/必需。

    【讨论】: