Search code examples
iosnsurlconnectionnsurlconnectiondelegate

How to change icons based on the percentage of download completed?


In iOS, how do I change the icon based on the percentage of download completed so that the user can get experience of downloading?

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    self.downloadedContentLength+=[data length];//data downloaded.
    double percent = ((double)self.downloadedContentLength/self.contentLength)*100;//percentage of data downloaded
    NSLog(@"PERCENT = %f", percent);
    if (percent<15) {//if percent is < 15 show image1
           //show image1
    }
    else if (percent<30)//if percent is < 30 show image2
    {
        //show image2
    }
}

Each time didReceiveData is hit it should calculate the percentage of data downloaded and based on that the image has to be changed.


Solution

  • - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        self.downloadedContentLength+=[data length];//data downloaded.
        double percent = ((double)self.downloadedContentLength/self.contentLength)*100;//percentage of data downloaded
    
        UIImageView *downloadImage = [UIImageView alloc] init]; <- Create a Frame
        NSLog(@"PERCENT = %f", percent);
        if (percent<15) {//if percent is < 15 show image1
               downloadImage.image = [UIImage imageNamed:@"IMAGE_NAME"]; 
        }
        else if (percent<30)//if percent is < 30 show image2
        {
               downloadImage.image = [UIImage imageNamed:@"IMAGE_NAME"];
        }
    }