3

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];
Brodie
  • 3,526
  • 8
  • 42
  • 61

2 Answers2

1

You could override connection:didReceiveData: in your NSURLConnection call. See here for more:

How to integrate NSURLConnection with UIProgressView?

Community
  • 1
  • 1
Joost Schuur
  • 4,417
  • 2
  • 24
  • 39
  • There is no explicit NSURLConnection call in "[NSData dataWithContentsOfURL:]", can you give a pointer to a full download-with-progress implementation that starts with just the URLstring? – Bogatyr Feb 17 '11 at 10:11
  • or ready solution http://www.developers-life.com/progress-bar-download-file-on-iphone.html – Volodymyr B. Jul 06 '12 at 16:17
0

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.

jrtc27
  • 8,496
  • 3
  • 36
  • 68