6

I get the image url from webservice,i has loaded that image in imageview.Like as follows..

[ImageName setImageWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"image url"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]]].

It has taking time to load image in imageview. I want to display activity indicator before loading image in imageview.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
user3051331
  • 71
  • 1
  • 7
  • check this [link](http://stackoverflow.com/questions/19831885/set-progress-bar-for-downloading-nsdata) you need to modify the logic in setImageWithURL. – Pawan Rai Mar 08 '14 at 06:29

1 Answers1

13

You could do it this way :

Add an indicator to your view, place it at the center of your imageview

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicator startAnimating];
    [indicator setCenter:self.imageView.center];
    [self.contentView addSubview:indicator];

Remove the indicator from the superview in the block's succes method.

     NSUrl *anURL=[NSURL URLWithString:[[NSString stringWithFormat:@"image url"]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]];

    [_imageView setImageWithURL:[NSURL URLWithString:anURL]
                       success:^(UIImage *image) {
                           [indicator removeFromSuperview];
                       }
                       failure:^(NSError *error) {

                       }];
}

Of course you could make a nice subclass for this

Pawan Rai
  • 3,434
  • 4
  • 32
  • 42