如何根据下载完成百分比更改图标?

时间:2015-08-19 17:28:53

标签: ios nsurlconnection nsurlconnectiondelegate

在iOS中,如何根据已完成下载的百分比更改图标,以便用户可以获得下载体验?

- (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
    }
}

每次didReceiveData被点击时,都应该计算下载数据的百分比,并根据图片进行更改。

1 个答案:

答案 0 :(得分:1)

- (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"];
    }
}