Alamofire-image 在 uicollectionview 单元格中加载错误的图像

Aja*_*kur 5 alamofire swift2 alamofireimage

我一直在使用 alamofire-image 库在 uicollectionview 单元格中显示来自 url 的图像。

我注意到大部分时间它显示正确的图像,但有时显示错误的图像(大部分时间是前一个单元格图像)。如何克服这个问题。

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

          let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath) as! myCollectionViewCell

          cell.titleLabelCell.text = element[indexPath.row]["title"]
          cell.generLabelCell.text  = element[indexPath.row]["gener"]

          let URL = NSURL(string: "\(element[indexPath.row]["banner_site_url"])" )!

          cell.mImageView.af_setImageWithURL(URL)

          return cell
      }
Run Code Online (Sandbox Code Playgroud)

Bob*_*eld 4

当您重用该单元时,您必须中断与可能正在进行的任何图像下载请求的连接。

您可以在以下位置找到Todd Kramer编写的优秀教程:

使用 Alamofire 在 Swift 中异步下载、缓存和解码图像:第 1 部分

请注意单元格视图类PhotoCollectionViewCell的实现。它调用成员函数reset,该函数在启动对当前图像的请求之前取消任何现有请求。

如果图像很小并且可能被重复使用,我倾向于取消单元格更新而不是检索。

阅读全文。