我有一个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;
}
然而,当我滚动时,它会猛拉/断断续续。它根本不光滑,滚动可能很痛苦。
我怎样才能让这更顺畅?
答案 0 :(得分:0)
只需在背景中使用GCD加载图片,即可让用户界面更具响应性:
- (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;
}