collectionView 中的图像在滚动时发生变化

Images in the collectionView are changing while scrolling

我在 xcode 8.3 工作。我有一个集合视图。我已经从网络服务下载图像并将图像放在每个 CollectionView 单元格中。但是当我滚动 CollectionView 时,每个单元格中的图像都在变化。几分钟后,它显示正确的图像。我已经尝试了 Whosebug 中可用的许多解决方案。但我没有得到解决方案。请帮我。

这是因为重复使用了细胞。尝试重置单元格中的图像 class prepareForReuse 方法

-(void)prepareForReuse {
    [super prepareForReuse];
    self.imageView.image = nil;
}

您遇到的问题是由于重用了 UITableViewCell。 如果您从 Web 服务下载图像,请使用 AlamofireImage or SDWebImage。它会解决你的问题。

就像其他人所说的那样,这很可能是因为您正在将可重复使用的单元格出队(正如您应该做的那样)并将 cell.imageView.image 属性 设置为您的网络图像。

这里的问题是,因为 iOS 通过 "reusing" 这些单元格节省了内存,所以它们实际上是内存中的相同单元格。因此,当它滚出屏幕的一个边缘并消失时。当新单元格继续滚动而不是创建一个新的单独单元格时,它只是使用刚刚离开屏幕的已有单元格。这意味着您的旧图像仍然显示在单元格中。

标准做法是在 cellForRowAtIndexPath: 方法中设置单元格的内容。但是,如果您将其设置为异步获取的图像,则在获取新图像之前,单元格完全有可能(很可能)与旧图像一起出现在屏幕上。据推测,一旦图像被下载,它就不再是一个问题了,因为它们应该立即从缓存中 return。

此处的简单解决方法是在每次在单元格中设置图像之前将图像置零,或者最好使用占位符图像。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL_IDENTIFIER" forIndexPath:indexPath];

    // This will clear any existing image from the cell about to be displayed it is an option
    //[cell.imageView setImage:nil];

    if (ImageExistsLocally)
    {
        cell.imageView.image = [UIImage imageWithContentsOfFile:@"SomeImagePath"];
    } 
    else 
    {
        [cell.cellImageView sd_setImageWithURL:yourRemoteImage.thumbnailUrl
                              placeholderImage:[UIImage imageNamed:PlaceHolderImageName]
                                     completed:nil];
    }
    return cell;
}

请注意,sd_setImageWithURL 来自 SDWebImage 库,我认为这里有人提到过。 https://cocoapods.org/pods/SDWebImage

UICollectionView 重复使用相同的 UICollectionViewCell 来提高性能。只有 UICollectionViewCell 中的数据被改变,所以在使用 UICollectionViewCell 之前,必须清除 UICollectionViewCell 之前的数据。 Cocoa Framework 提供了一个存在于 UICollectionViewCell 中的方法,该方法在每次要重用 UICollectionViewCell 时触发。

只需在自定义 UICollectionViewCell class 文件

的 .m 文件中覆盖下面给出的函数
-(void)prepareForReuse {
    [super prepareForReuse];
// here we clear the previously set data
    self.imageView.image = nil; // this will clear the previously set imageView is a property of UIImageView used to display a image data
}

您可以使用prepareForReuse()方法来处理这个问题。

override func prepareForReuse() {
    super.prepareForReuse()

    // still visible on screen (window's view hierarchy)
    if self.window != nil { return }

    imageView.image = nil
}

注意:如果您使用的是SDWebImage,则应添加此行以取消当前的单元格图像下载。

imageView.sd_cancelCurrentAnimationImagesLoad()