I'm download an array of images to create animation. How can I make when download starts to show activity indicator and when it finishes to hide activity indicator.
self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
img.animationImages = [NSArray arrayWithObjects:
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
img.animationDuration = 20.0f;
img.animationRepeatCount = 0;
[img startAnimating];
[self.view addSubview: img];
Your images are downloading synchronously which means that you're blocking the main thread (this is bad). Instead, run the downloading on a background thread which will allow the main thread, and in extension, the UI of your app to still function. Using your example code, here is what it would look like when run in the background:
self.img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.width-70)];
self.img.animationDuration = 20.0f;
self.img.animationRepeatCount = 0;
[self.img startAnimating];
[self.view addSubview:self.img];
// START ANIMATING YOUR ACTIVITY INDICATOR HERE
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSArray *images = @[
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]]],
];
dispatch_async(dispatch_get_main_queue(), ^(void) {
// STOP ANIMATING YOUR ACTIVITY INDICATOR HERE
[self.img setAnimationImages:images];
});
});