我在我的視圖控制器之間異步加載圖像時遇到問題。我有一個tableView控制器,當你點擊一行時打開一個集合視圖控制器。當收集視圖打開時,圖像從互聯網下載時會暫停一段時間。UICollectionViewController多線程圖像加載
我把下面的代碼放到了collectionView:cellForItemAtIndexPath:中,但是這會導致同步加載,並使程序滯後。
-(UICollectionViewCell *) collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"CollectionCell";
CollectionCell *cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSData *imageData = [NSData dataWithContentsOfURL:[self.theImage objectAtIndex:indexPath.row]];
cell.itemImage.image = [UIImage imageWithData:imageData];
return cell;
}
我的問題是,其中和如何實現代碼,這樣集合視圖加載空單元格,而圖像下載(然後他們出現下載後)?
我試圖使用大中央調度,但我無法完全弄清楚。如果有人能幫助我指出正確的方向,我會很感激。
你想異步地下載一個線程,這不是主線程。看看蘋果的IconDownloader:http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Listings/Classes_IconDownloader_m.html#//apple_ref/doc/uid/DTS40009394-Classes_IconDownloader_m-DontLinkElementID_6 –
我有collectionview中有多個圖像,並且都大於圖標的大小。這仍然適用? – mrcharlie
當然是的。這正是你在找什麼;) –