ios - 使用 Alamofire 和 AlamofireImage 延迟加载图像在第一次加载时不工作

标签 ios swift uicollectionview alamofire alamofireimage

所以我试图在这个应用程序中遵循 mvc 架构。这是图像部分的代码。

型号

   import Alamofire
   import AlamofireImage

    class Brands {
    private var _image : UIImage!
    var image : UIImage {
        if _image == nil {
           _image = UIImage(named: "loadingImage")
        }
        return _image
    }

    init(BrandsDict : Dictionary<String, AnyObject>){
      if let imageUrl = BrandsDict["imageUrl"] as? String{
         Alamofire.request(imageUrl).responseImage(completionHandler: { (response) in
                    guard let image = response.result.value else {
                        self._image = UIImage(named: "loadingImage")
                        return
                    }
                    self._image = image

                 })
       }else {
       self._image = UIImage(named : "loadingImage")
    }

查看

class BrandsCVCell : UICollectionViewCell {
  @IBOutlet weak var BrandImage : UIImageView!

  var brand : Brands!

  func configureCell(_ brand : Brands){
   self.brand = brand
   BrandImage.image = self.brand.image
}
}

Controller

inViewDidLoad ....

if let jsonArray = data as? NSArray {
                for objects in jsonArray {
                    let Brand = Brands(BrandsDict: objects as! Dictionary<String, AnyObject>)
                    self.Brands.append(Brand)
                }
               self.bestBrandCollection.reloadData()
            }

....

if collectionView == BrandCollection {
 if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BrandsCell", for: indexPath) as? BrandCollectionViewCell {

                let Brand = Brands[indexPath.row]
                cell.configureCell(Brand)
                return cell
            }else {
                return UICollectionViewCell()
            }
}

问题是,当图像加载到 Collection View 中时,正在显示的单元格不会获取下载的图像,但是当我滚动它们时,较早的单元格会获取它们的图像。下载后有人可以帮我延迟加载图像吗? (也许是完成处理程序,但我不知道把它放在哪里)。编码答案将不胜感激。

最佳答案

问题是从网络下载的图像在下载后没有刷新到单元格中。您需要在 Alamofire.request block 中回调。解决方案:

首先,在模型中添加回调 block :

class Brands {
    //......
    public var imageDidDownload:(()->Void)?  //Key point, declare a callback block

    init(BrandsDict : Dictionary<String, AnyObject>){
        if let imageUrl = BrandsDict["imageUrl"] as? String{
            Alamofire.request(imageUrl).responseImage(completionHandler: { (response) in
                //......
                self._image = image
                self.imageDidDownload?() //Key point, callback after image downloaded
                //......
            })
        }else {
            //......
        }
    }
}

其次,在cell中,处理图片下载回调刷新图片:

class BrandsCVCell : UICollectionViewCell {
    //......
    func configureCell(_ brand : Brands){
        self.brand = brand
        self.brand.imageDidDownload = { [weak self]() -> Void in
            self?.BrandImage.image = self?.brand.image  //Key point, refresh image to the imageView after downloading.
        } 
        BrandImage.image = self.brand.image
    }
}

尝试一下,应该可以。

关于ios - 使用 Alamofire 和 AlamofireImage 延迟加载图像在第一次加载时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44321355/

相关文章:

objective-c - 在 sprite.runAction 之后调用 Block 时崩溃

iphone - 如果定义了 didFinishPickingMediaWithInfo,相机会卡住

ios - tableheaderview的滚动过程如何实现?

ios - 如何在 swift 2.2 中捕获 NSUnknownKeyException?

ios - UICollectionView 显示隐藏动画问题

arrays - 追加包含其他类数组的类数组

ios - 如何在交互发生时禁用 UICollectionView 中的用户交互

iOS 在一行中包含数据的两个单元格

javascript - 通过绑定(bind)到 div 的 onclick 事件在 iPad 上加载 HTML5 视频

swift - 使用 Swift 在 Web View 中点击电话号码时显示警报 View