2010-06-18 29 views
0

我的代碼有問題,它似乎永遠不會正確執行。將URL加載到UIImageView中時,代碼沒有按預期執行

我試着從UIActivity,滑塊,UITextVieweer等等很多東西......但它永遠不會改變,

的代碼是用在Xcode基於導航的應用程序運行。 loadingTview是一個TextView,

的問題是,看到loadingTview是,永遠不會奏效,它總是掛,用戶按下一個按鈕,執行該代碼。 loadingTview是一個文本視圖,用於在頁面下載圖像時使用0.4的alpha值進行「加載」,人們知道它的加載。

我試過的意見很好,但同樣的問題。

我怎麼能進步呢?

loadingTview.hidden = false; 
today = [NSDate date]; 
dateFormat = [[NSDateFormatter alloc] init]; 
[dateFormat setDateFormat:@"dd-MM-yyyy"]; 
dateString = [dateFormat stringFromDate:today]; 


if (PageEntered == @"page1") 
{ 
    NSString *url = [NSString stringWithFormat:@"http://www.imagegoeshere.com/%@.jpg",dateString]; 
    imageURL = [NSURL URLWithString:url]; 
    imageData = [NSData dataWithContentsOfURL:url]; 
    image = [UIImage imageWithData:imageData]; 
    FullScreenImage.image = image; 
    loadingTview.hidden = true; 
    [navigationController pushViewController:vFullscreen animated:YES]; 
} 

回答

0

我不知道完全的問題是什麼,但我認爲,當你從視圖2去,直到圖像實際上是打開VIEW3顯示加載屏幕,右側前加載到VIEW3它「掛起」的視圖2?

如果是這樣的話,那麼你需要做的是加載圖像在不同的線程使加載不會顯示加載屏幕擋住VIEW3。

看一看NSThread(雖然有清潔劑/更好的方法來做到這一點)。

基本上做到這一點在VIEW3的控制器:

- (void) viewDidLoad { 
    // <First, show your 'Loading...' screen here> 
    // Then create a thread to load the image: 
    [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil]; 
} 

// Then somewhere in the same class define the loading method: 
- (void)loadImage { 
    // Remember to create a new autorelease pool for this thread. 
    // <Load your image here> 

    // When image is done loading, call the main thread 
    [self performSelectorOnMainThread:@selector(imageDoneLoading) withObject:nil waitUntilDone:YES]; 
} 

// Then define the method to call when the image is done 
- (void) imageDoneLoading { 
    // Hide the 'Loading...' screen. 
} 

如果這不是你有問題,那麼請提供更多的細節,什麼是真正發生什麼問題。

祝你好運。

+0

太棒了!那正是問題所在:)我今天晚上閱讀了NSThread的文檔! – user370507 2010-06-18 20:30:30

0

我真的不明白你的問題,但我確實看到了幾乎肯定是錯的東西。這條線:

if (PageEntered == @"page1") 

應該是這樣的:

if ([PageEntered isEqualToString:@"page1"]) 

的Objective-C沒有做運算符重載,所以你的代碼做一個指針的比較,而不是一個值比較。

+0

謝謝你,生病得到改變,現在。 好吧,虐待解釋更多關於我的問題。 我有一個應用程序,它使用導航控制器。我有3個視圖控制器。 View1 View2 View3可以說。當用戶單擊View1上的按鈕時,它將轉到view2,然後當用戶單擊view2上的按鈕時,它會轉到view3,它具有一個UIImageViewer,它從Internet上加載數據,大圖像文件。我想要的是,當用戶在view2上舔按鈕時,在下載圖像時顯示加載頁面,然後下載一次,移除該加載圖像並顯示view3。 – user370507 2010-06-18 18:12:49