When moving from one view to another, I want in my second view as long as the image is being downloaded to see a spinner (so that the second view will open instantly not take any time at all, and the user waits for the spinner to finish).
Here is my code:
In the second View, the one that opens:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center=CGPointMake(160.0,240.0 );
spinner.hidesWhenStopped=YES;
[self.view addSubview:spinner];
[spinner startAnimating];
NSString *urlString=[NSString stringWithFormat:@"http://mysite.com/projects/test/test.jpg"];
NSLog(@"URL is:%@",urlString);
NSURL *url=[NSURL URLWithString:urlString];
[more_info_image setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:url]]];
[spinner stopAnimating];
}
I do not see the spinner at all. i do not think that this is because my internet connection is so fat. On the other hand, I see a little delay when a button is pressed and waiting for the view to open – like that it is that time when it tries to download the image.
I want to open the view, have the spinner, download the image and then let the spinner go away.
Must I change anything?
You might not be seeing the
UIActivityIndicatorViewsince you are using[NSData dataWithContentsOfURL:url]on the main thread. This is blocking the main thread from displaying theUIActivityIndicatorViewuntil after the image is downloaded, and by that point you are removing it.You might want to do something like:
but make sure you define
spinnerin your*.hfile for this to work.The other way is to use Grand Central Dispatch and do something like: