views:

89

answers:

2

My viewDidAppear methods is in below. My view has a activieIndicator that is animating and a imageView. In viewDidAppear I load an image from my server which will be used as an image of the above imageView. I want until my image is fully loaded, my view will show the animating indicator. But I can not do it. Until full image load, i can not see my view. during image loading i see a black view.

-(void)viewDidAppear
{
     NSLog(@"view did appear");
     [super viewDidAppear:animated];
     NSData *data=[NSData dataWithContentsOfURL:[NSURL          
     URLWithString:@"http://riseuplabs.com/applications/christmaswallpaper/images/1.jpg"]];
}

is there any solution???

+1  A: 

You are loading the image data on the main thread, and so your thread will be blocked until the data has been fully loaded. You can overcome this problem by downloading the data on a background thread.

-(void)viewDidAppear {
    // other code here

    // execute method on a background thread
    [self performSelectorInBackground:@selector(downloadData) withObject:nil];
}

- (void)downloadData {
    // data is declared as an instance variable in the header
    data = [NSData dataWithContentsOfURL:[NSURL 
        URLWithString:@"http://riseuplabs.com/applications/christmaswallpaper/images/1.jpg"]];

    // All UI updates must be done on the main thread
    [self performSelectorOnMainThread:@selector(updateUI)
        withObject:nil waitUntilDone:NO];
}

- (void)updateUI {
    // Hide the spinner and update the image view
}
nduplessis
+1  A: 

Look at how to download data asynchronously. See here.

@nduplessis's answer is a good one, and is easier, although there are benefits to using NSURLConnection asynch. His answer would be the way to go if you are in a hurry and don't want to implement a new technique.

And please consider making it a practice to select an answer from all the responses to your questions. It helps build our community here.

mahboudz
@mahboudz is correct when saying there are some benefits to setting up async data downloads, especially if you want to be able to track things like the expected content size and the number of bytes that have been received
nduplessis