滚动时CollectionView Jerks / Stutters

时间:2014-05-26 15:53:40

标签: ios uitableview uicollectionview

我有一个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;
}

然而,当我滚动时,它会猛拉/断断续续。它根本不光滑,滚动可能很痛苦。

我怎样才能让这更顺畅?

1 个答案:

答案 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;
}