UICollectionView 图像闪烁

UICollectionView image flickering

我有一个全屏显示单元格的 UICollectionView(即画廊)。 有时,当滑动新图像时,显示的图像不是正确的图像不到一秒钟,图像就会更新。 其他时候,会显示错误的图像,但如果您向左滑动而不是向右滑动(即图像消失并重新出现),则会出现正确的图像。 我不明白这种行为的原因。 当集合视图需要图像时,图像会异步下载。 这是代码:

 let blank = UIImage(named: "blank.png")
 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell

    let image = images[indexPath.row]
    if let imageView = cell.viewWithTag(5) as? UIImageView {
        imageView.image = blank
        if let cachedImage = cache.objectForKey(image.url) as? UIImage {
            imageView.image = cachedImage
        } else {
            UIImage.asyncDownloadImageWithUrl(image.url, completionBlock: { (succeded, dimage) -> Void in
                if succeded {
                    self.cache.setObject(dimage!, forKey: image.url)
                    imageView.image = dimage
                } 
            })
        }
    }

    return cell
}

其中 UIImage.asyncDownloadImageWithUrl 是:

extension UIImage {
   static func asyncDownloadImageWithUrl(url: NSURL, completionBlock: (succeeded: Bool, image: UIImage?) -> Void) {
       let request = NSMutableURLRequest(URL: url)
       NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response, data, error) in
           if error == nil {
               if let image = UIImage(data: data) {
                  completionBlock(succeeded: true, image: image)
               }
           } else {
                  completionBlock(succeeded: false, image: nil)
           }

       })
     }
  }

对于显示的第一张图片:

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if let index = self.index {
        let newIndexPath = NSIndexPath(forItem: index, inSection: 0)
        self.collectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        self.index = nil
    }
}

这就是我认为正在发生的事情:

  • 单元格 A 出现
  • 图像 A 请求加载到单元格 A
  • 单元格 A 从屏幕上消失
  • 单元格 A 重新出现(被重复使用)
  • 请求图像 B 加载到单元格 A
  • 图像 A 的请求已完成
  • 图像 A 加载到单元格 A
  • 图像 B 的请求已完成
  • 图像 B 加载到单元格 A

发生这种情况是因为您没有跟踪 url 应该在完成块上加载哪个图像。

试试这个:

一种方法是将图像的 url 存储在 UICollectionViewCell 中。为此,创建一个子类:

class CustomCollectionViewCell:UICollectionViewCell {
    var urlString = ""
}

然后:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell

    let image = images[indexPath.row]
    if let imageView = cell.viewWithTag(5) as? UIImageView {
        imageView.image = blank
        cell.urlString = image.url.absoluteString
        if let cachedImage = cache.objectForKey(image.url) as? UIImage {
            imageView.image = cachedImage
        } else {
            UIImage.asyncDownloadImageWithUrl(image.url, completionBlock: { (succeded, dimage) -> Void in
                if succeded {
                    self.cache.setObject(dimage!, forKey: image.url)
                    //
                    // This can happen after the cell has dissapeared and reused!
                    // check that the image.url matches what is supposed to be in the cell at that time !!!
                    //
                    if cell.urlString == image.url.absoluteString {
                        imageView.image = dimage
                    }

                } 
            })
        }
    }

    return cell
}

为了更准确的回复,post 项目(或它的准系统版本)。重现您的设置和测试需要大量工作。