2009-11-09 132 views
0

我的viewDidAppear方法在下面。 我的看法有一个activieIndicator,它是动画和一个imageView。 在viewDidAppear中,我从我的服务器加载一个图像,它将用作上述imageView的图像。 我想直到我的图像完全加载,我的视图将显示动画指标。 但我做不到。 直到完整图像加载,我看不到我的看法。 在图像加载过程中,我看到一个黑色的视图。查看显示延迟

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

是否有解决方案???

回答

1

您正在将图像数据加载到主线程中,因此线程将被阻塞,直到数据完全加载。您可以通过在后台线程上下载数据来解决此问题。

-(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 
} 
1

看看如何异步下载数据。见here

@nduplessis的答案是一个很好的答案,并且更容易,尽管使用NSURLConnection asynch有好处。如果你匆忙而又不想实施新技术,他的回答就是要走的路。

并请考虑让它成为一个练习,从您对问题的所有回答中选择一个答案。它有助于我们在这里建设社区。

+0

@mahboudz在说明设置异步数据下载有什么好处时是正确的,特别是如果您希望能够跟踪预期的内容大小和已收到的字节数 – nduplessis