2013-02-04 38 views
1

該代碼位於單元初始化例程中,該例程設置自定義單元的元素。它異步地從網上獲取圖像。但是一旦完成,我需要重新繪製它。異步獲取圖像並設置setNeedsDisplay

這是我的代碼片段:

dispatch_async(myCustomQueue, ^{ 

    //Look for the image in a repository, if it's not there 
    //load the image from the web (a slow process) and return it 
    mCover.image = [helperMethods imageManagerRequest:URL]; 

    //Set the image to be redrawn in the next draw cycle 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [mCover setNeedsDisplay]; 
    }); 

}); 

但不重繪的UIImageView。我試圖重繪整個單元格,但這也不起作用。非常感謝您的幫助。我一直在試圖解決這個問題一段時間!

+1

你返回一個UIImage或UIImageView的?也許你無意中將圖像分配給圖像視圖? – Dave

回答

3

而不是setNeedsDisplay,你應該在主線程上設置圖像,蘋果已在their documentation中提到過。

注意:大部分UIKit類只能在 應用程序的主線程中使用。對於從UIResponder派生的類 或者涉及以任何方式操縱應用程序的用戶界面的情況尤其如此。

這應該可以解決你的問題:

dispatch_async(myCustomQueue, ^{ 

    //Look for the image in a repository, if it's not there 
    //load the image from the web (a slow process) and return it 
    UIImage *image = [helperMethods imageManagerRequest:URL]; 

    //Set the image to be redrawn in the next draw cycle 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     mCover.image = image; 
    }); 

});