2009-11-09 128 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