Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to show activity indicator before image loading in ios

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.

like image 959
user3051331 Avatar asked Mar 08 '14 06:03

user3051331


1 Answers

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

like image 87
Pawan Rai Avatar answered Nov 20 '22 11:11

Pawan Rai