2016-08-15 1 views
0

J'ai un UITableView avec une image dans chaque cell. Quand je clique sur le cell, l'image sera téléchargée. Je souhaite afficher l'indicateur d'activité jusqu'à ce que l'image ne soit pas téléchargée.Comment exécuter l'indicateur d'activité jusqu'au téléchargement de l'image est terminée

Voici le code

code TableView

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

code PicturePerformGesture

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]; 

Avant de télécharger

pendant le téléchargement
enter image description here

+0

Utilisez MBProgressHud https://github.com/jdg/MBProgressHUD –

+0

votre code postal de 'TableView' et où vous téléchargez l'image. – Dravidian

+0

S'il vous plaît poster le code dans la méthode 'PicturePerformGesture' –

Répondre

3

pendant que vous créez l'image cellulaire à ce moment-là, vous devez vérifier l'image est téléchargée ou non si OUI qui peut taper sur l'image autre ActivityIndicator de départ sage et pendant que vous remplissez l'image télécharger vous pouvez arrêter ActivityIndicator pour la meilleure expérience de téléchargement d'image, utilisez la bibliothèque SDWebImage 3rd party et obtenez tous les statuts d'image mentionnés ci-dessous.

Utilisation de l'achèvement Bloc:

// 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. 

}]; 

Une autre façon d'obtenir progrès de l'image utilisation de téléchargement ci-dessous bloc de 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 Lien: https://github.com/rs/SDWebImage

Ou vous pouvez utiliser votre propre code pour télécharger l'image à l'aide de GCDdispatch_async comme mention ci-dessous.

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 
        }); 
       }); 

J'espère que cela vous aidera.

+0

c'est Solve merci – ItsMeMihir

0

Commencez l'animation avant le code de bloc de téléchargement d'image avec cette méthode.

[myActivityIndicator startAnimating]; 

Et l'arrêter dans votre image en utilisant le téléchargement de blocs iOS comme celui-ci ...

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) { 

         } 
        }];