tags:

views:

206

answers:

2

i trying to download the picture from URL, and use a activity indicator animating to present the file is downloading, however, it is not working as my indicator wont animating when i call this download function,can somebody tell me why?

-(void)download{
    [indicator startAnimating];
    NSString *downloadPath=@"http://www.xyz.com/path/pic.jpg; 
    NSData *downloadData=[NSData dataWithContentsOfURL:[ NSURL  URLWithString:downloadPath]];
    if(downloadData){
           //do something
          [indicator stopAnimating];
     }
      else{
        //do something
        [indicator stopAnimating];
          }

}

A: 

You need to put [indicator startAnimating] and [indicator stopAnimating]; in separate methods. I believe the animation does not kick in until the method reaches its end. So if you separate this into several methods this should work

  • One method that starts your animation
  • One method that downloads the file.
  • One method that stops the animation.

Another option is threading to acomplish this. More information here

Benjamin Ortuzar
A: 

The animation is performed in the event loop, which is in the same thread as your code. That is, the animation won't start while your code is executing.

Instead you either need to forget about the animation, switch to using the asynchronous download methods or perform the download in a separate thread. I'd recommend the async option.

Stephen Darlington