【问题标题】:CollectionView images are flashing while scrollingCollectionView 图像在滚动时闪烁
【发布时间】:2016-06-03 12:40:15
【问题描述】:

我在 collectionView: cellForItemAtIndexPath: 中使用以下代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = nil;
    if ([ImageArray count] >0){
        
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
                NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
                UIImage *image = [UIImage imageWithData: data0];
                
                dispatch_sync(dispatch_get_main_queue(), ^(void) {
                    recipeImageView.image = image;
                });
            });
        
    }
    
    [spinnerShow stopAnimating];
    
    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    return cell;
}

问题是,如果我滚动集合视图,图像会闪烁。为什么呢?滚动时如何防止图像闪烁?

【问题讨论】:

  • 隐藏这些行 cell.layer.shouldRasterize = YES; cell.layer.rasterizationScale = [UIScreen mainScreen].scale;并尝试一次

标签: ios objective-c uicollectionview


【解决方案1】:

我认为根本原因是加载下面 2 行图像的成本:

NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];

我的解决方案是使用 AFNetworking 加载图像过程,请参考链接:https://github.com/AFNetworking/AFNetworking

安装 AFNetworking 后,您只需:

if ([ImageArray count] >0){
    [recipeImageView setImageWithURL:[NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]] placeholderImage:nil];
}

希望你喜欢这个!

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    相关资源
    最近更新 更多