I have an app that downloads a picture from a server. I would like to show the progress to the user. I have read about UIProgressView on the apple docs and read many answers on this site but I can't get it to work. Here is my code
In my viewDidLoad
I have
_profileProgressView.hidden= YES;
_profileProgressView.progress = 0.0;
In my - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
I show the UIProgressView and then get the expected size of the image.
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.profileProgressView.hidden= NO;
[self.remote_response setLength:0];
received = 0;
self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
NSLog(@"Content Length::%@", self.filesize);
}
In my - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d
, I calculate the percentage of the image downloaded and then update the progress of the UIProgressView and log the download progress.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
[self.remote_response appendData:d];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.remote_response length]];
dispatch_async( dispatch_get_main_queue(), ^ {
float progress = (_profileProgressView.progress + ([self.filesize floatValue]/[resourceLength floatValue])*100);
[self.profileProgressView setProgress:progress animated:YES];
NSLog(@"Downloading %.0f%% complete", _profileProgressView.progress);
});
}
When I run this code
The UIProgressView never shows up
The log I get is
Downloading 0% complete
. I expected to get the log of the image being downloaded.
Also I thought I should alloc
and init
the UIProgressView in the viewDidDownload
, when I do that I get UIProgressView but it does not show the progress and it does not hide after the image is done being downloaded.