Malicia
Malicia

Reputation: 154

Show/Hide Activity indicator when i load url image from a Json using Afnetworking

This is my method to load my json with afnetworking:

-(void) connectPhp {

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager GET:@"http://XXXXXXXXXXXXX.php" parameters:nil success:^(AFHTTPRequestOperation *operation, NSArray * responseObject) {

        i=0;
        test = (NSDictionary *)responseObject;

        compteur =test[@"images"][@"items"];
        total=[compteur count];
        NSLog(@"%d TOTO", total);
        // AFFICHER LE TOTAL D'URL DANS TOTAL NSLog(@"%d", total);


        NSLog(@"%@", test[@"images"][@"items"][i][@"title"]);

        NSString *urltest = test[@"images"][@"items"][i][@"url"];
        self.label.text = test[@"images"][@"items"][i][@"title"];

        url = [NSURL URLWithString:urltest];

        data = [NSData dataWithContentsOfURL:url];
        img = [[UIImage alloc] initWithData:data];

        //Je stocke le total d'images
        totalImg = [NSString stringWithFormat:@"%d", total];


        //Je stocke l'ID de l'image
        idImg = [NSString stringWithFormat:@"%d", i+1];


        NSLog(@"%@", idImg);
        //je donne au compteur la valeur idImg
        self.compteurID.text=idImg;
        self.compteurTotal.text=totalImg;

        if (total<9){

            [self.pageControl setNumberOfPages:total];
            [self.pageControl setCurrentPage:i];
        }
        else if (total>9){
            [self.pageControl setNumberOfPages:10];
            NSLog(@"ICI TOTAL>10");

        }



    }

    failure:nil];

}

This is my method to switch my images on drag:

-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.scrollView.contentOffset.x<-20){
        if (i==0){[self resizeImages];}

        else{
            i--;
            [self resizeImages];
        }
    }
    else if ((self.scrollView.bounds.size.width)-(self.scrollView.contentOffset.x)==25){
        if (i==(total)-1){i=0;}
        i++;
        [self resizeImages];

    }
    else if (self.scrollView.contentOffset.x>(self.imgView.frame.size.width)/3){
        if (i==(total)-1){i=0;}
        i++;
        [self resizeImages];

    }
}

It works. When i drag, i load the next url of image in my json. It works. However, I would like to show an activity indicator when i drag and when my next image is loading. How can i do that?

Upvotes: 0

Views: 529

Answers (1)

Aaron Brager
Aaron Brager

Reputation: 66282

  1. When you start loading the image, add a UIActivityIndicatorView to your view hierarchy and start the animation.
  2. When your image finishes downloading, send the activity indicator a removeFromSuperview message.

Upvotes: 1

Related Questions