开发者

View showed delay

开发者 https://www.devze.com 2022-12-11 01:49 出处:网络
My viewDidAppear methods is in below. My view has a activieIndicator that is animating and a imageView.

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开发者_C百科/images/1.jpg"]];
}

is there any solution???


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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消