2010-06-24 105 views
3

如何添加進度條來下載大圖或任何文件?這裏是我的代碼:下載圖片時的進度條

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

回答

1

你可以覆蓋連接:didReceiveData:在你的NSURLConnection調用中。請參閱此處瞭解更多:

How to integrate NSURLConnection with UIProgressView?

+0

「[NSData dataWithContentsOfURL:]」中沒有顯式的NSURLConnection調用,你能給出一個指針,指向一個完整的帶有進度的下載實現,它只以URLstring開始? – Bogatyr 2011-02-17 10:11:54

+0

或準備好的解決方案http://www.developers-life.com/progress-bar-download-file-on-iphone.html – SAKrisT 2012-07-06 16:17:21

0

在後臺運行一個選擇器,檢查圖像的下載大小,並將其與所需大小進行比較。然後將進度條的%設置爲已下載/需要。

+0

如何檢查尺寸? – Brodie 2010-06-24 20:00:42