ItsMeMihir
ItsMeMihir

Reputation: 294

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

Upvotes: 0

Views: 697

Answers (2)

CodeChanger
CodeChanger

Reputation: 8341

While You create Image Cell at that time you need to check Image is downloaded or not if YES that can tap on image otherwise starts 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.example.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.

Upvotes: 3

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

Start animating before the image download block code with this method.

[myActivityIndicator startAnimating];

And stop it in your image downloading using iOS blocks like this...

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) {
                        [myActivityIndicator startAnimating];
                        if (image && finished) {

                        }
                    }];

Upvotes: 0

Related Questions