[英]CollectionView Jerks/Stutters When Scrolling
我有一個UICollectionView
,它顯示播放列表的列表和播放列表中其中一首歌曲的圖像。
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
playlistCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
if(!cell)
{
cell = [[playlistCell alloc] init];
}
// Configure the cell...
MPMediaItem *rowItem = [[playlists objectAtIndex:indexPath.row] representativeItem];
UIImage *cellBG = [self getAlbumArtworkWithSize:CGSizeMake(320, 320) forPlaylist:[playlists objectAtIndex:indexPath.item]];
cell.image = cellBG;
return cell;
}
但是,當我滾動時它會抽動/結巴。 它一點都不平滑,滾動時可能會很痛苦。
我如何使它更平滑?
您可以通過使用GCD在后台加載圖像來使UI更具響應性,如下所示:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
playlistCell * cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
if(!cell)
{
cell = [[playlistCell alloc] init];
}
// Configure the cell...
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
MPMediaItem *rowItem = [[playlists objectAtIndex:indexPath.row] representativeItem];
UIImage *cellBG = [self getAlbumArtworkWithSize:CGSizeMake(320, 320) forPlaylist:[playlists objectAtIndex:indexPath.item]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.image = cellBG;
});
});
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.