未调用 SDWebImage 下载图像完成块

SDWebImage Download Image Completion Block not being called

所以 SDWebImage Download image and store to cache for key 这正是我想在我的应用程序中做的。我想将图像存储在一个键中并稍后获取图像。这是我的代码块:

SDWebImageManager.shared().imageDownloader?.downloadImage(with: URL.init(string: url), options: SDWebImageDownloaderOptions.highPriority, progress: { (receivedSize:Int,expectedSize:Int,url : URL?) in
  print("progressing here in this block ")
}, completed: ({(image : UIImage, data : Data,error : Error, finished : Bool)  in
  print("reached in the completion block ")

  if finished {
    SDImageCache.shared().store(image, forKey: "d", toDisk: true, completion: nil)
  } else {
    print(error.localizedDescription)
    showAlert(viewController: self, title: "", message: error.localizedDescription)
  }           
} as? SDWebImageDownloaderCompletedBlock))

但是,永远不会调用完成块。

使用这段代码:

SDWebImageManager.shared().loadImage(with: NSURL.init(string: individualCellData["cover_image"] as! String ) as URL?, options: .continueInBackground, progress: { (recieved, expected, nil) in
            print(recieved,expected)
        }, completed: { (downloadedImage, data, error, SDImageCacheType, true, imageUrlString) in
            DispatchQueue.main.async {
                if downloadedImage != nil{
                    self.yourImageView.image = downloadedImage
                }
            }
        })

而且您不必再次缓存图像,因为 SDWebImage 已经这样做了。要从缓存中检索图像,只需在此代码中使用相同的 url,如果图像存在,它将从缓存中获取图像。

更新代码:

如果您想使用自己的密钥,请使用 imageDownloader 而不是 loadImage :

SDWebImageManager.shared().imageDownloader?.downloadImage(with: NSURL.init(string: individualCellData["cover_image"] as! String ) as URL?, options: .continueInBackground, progress: { (recieved, expected, nil) in
                print(recieved,expected)
            }, completed: { (image, data, error, true) in
                print("Completed")
            })

print("Completed") 之后,如果没有错误,请使用您的缓存代码使用您的自定义密钥缓存图像。