Mr.MIHIR Mr.MIHIR - 3 months ago 26
iOS Question

How to run activity indicator till downloading the image is Complete

I have a

UITableView
with an image in every
cell
. When i click on the
cell
, image will be downloaded. I want to show activity indicator show till the image is not downloaded.

Here is Code

TableView Code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(PicturePerformGesture:)];
[cell.oMessageImg setUserInteractionEnabled:YES];
}


PicturePerformGesture Code

UIActivityIndicatorView *iActivity=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[iActivity startAnimating];
VideoData =[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:videoUrl] returningResponse:nil error:nil];
[VideoData writeToFile:fullPath atomically:true];
[iActivity stopAnimating];


Before Downloading



While Downloading

enter image description here

Answer

While You create Image Cell at that time you need to check Image is downloaded or not if YES that can tap on image other wise start ActivityIndicator on it and while you complete Image download you can stop ActivityIndicator for best Image Download experience use SDWebImage 3rd party library and get all image status as mention below.

Using Completion Block :

// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                ... completion code here ...

   // Add Your Stop Activity Code here.

}];

Another Way to Get Progress of Image Download use Below Block Of Code :

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:imageURL
                         options:0
                        progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                            // progression tracking code
                        }
                       completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                            if (image && finished) {
                                // do something with image
                                // Assign Image to ImageView and stop activity Indicator View
                            }
                        }];

SDWebImage Link : https://github.com/rs/SDWebImage

Or you can use your own code to Download image with the help of GCD dispatch_async as mention below.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
                    //  You may want to cache this explicitly instead of reloading every time.
                    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.loader.parsedData[indexPath.row] objectForKey:@"imageLR"]]];
                    UIImage* image = [[UIImage alloc] initWithData:imageData];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // Capture the indexPath variable, not the cell variable, and use that
                        UITableViewCell *blockCell = [tableView cellForRowAtIndexPath:indexPath];
                        blockCell.imageView.image = image;
                        [blockCell setNeedsLayout];
                        //Stop Your ActivityIndicator View hare 
                    });
                });

Hope it will help you.