2010-03-16 67 views
1

iphone中的圖像是否存在「on load complete」這樣的事情?uiimage oncomplete iphone

我希望能夠一次性銷燬UIActivity指示器並加載圖像。

這樣做的一般最佳做法是什麼?

回答

0

如果你只是想解僱UIActivityIndicatorViewUIImage已加載,並在UIImageView正常顯示,你可以嘗試以下方法:

- (void)loadImageAtPath:(NSString*)path 
{ 
    // show the indicator 
    [_myIndicatorView startAnimating]; 

    // give UIKit a chance to setup and draw the view hierarchy: 
    [self performSelector:@selector(_loadImageAtPath:) 
       withObject:path 
       afterDelay:0]; 
} 

- (void)_loadImageAtPath:(NSString*)path 
{ 
    // load the image 
    _myImageView.image = [UIImage imageWithContentsOfFile:path]; 

    // force the image to load and decompress now 
    _myImageView.image.CGImage 

    // image is ready, so we can remove the indicator view 
    [_myIndicatorView removeFromSuperview]; 
} 

我不得不承認,我沒有測試上面的代碼。如果它有效,請留下評論。

+0

再次感謝。我會盡快實施,並會讓你知道結果。 – dubbeat 2010-03-16 11:06:38

0

UIImage有一個同步API。這意味着當你創建(或繪製)一個UIImage時,它已經被加載和解壓。這也意味着創建(或繪製)UIImage的方法調用只要加載該圖像就會阻塞。

如果您想在加載圖像時執行其他操作,則必須使用Core Graphics C API在後臺線程上加載圖像。 UIImage不是線程安全的,只能在主線程上使用。

+0

感謝您的回覆尼古拉。這很有趣,說實話有點過頭了,因爲我缺乏經驗o-0 我也很想知道的是,我怎麼知道圖像何時完成加載,以便我可以刪除UIActivity指示器? 也許答案在你的回覆中,我只是不明白嗎? :) – dubbeat 2010-03-16 09:33:59