ios - 未调用 SDWebImage 下载图像完成 block

标签 ios swift image caching sdwebimage

所以 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))

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

最佳答案

使用这段代码:

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,如果图像存在,它将从缓存中获取图像。

更新代码:

如果您想使用自己的 key ,请使用 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") 之后,如果没有错误,请使用您的缓存代码使用您的自定义 key 缓存图像。

关于ios - 未调用 SDWebImage 下载图像完成 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47504465/

相关文章:

javascript - 上传前显示缩略图

html - 在网格布局中设置图像以在悬停时更改

android - 我应该如何获得所有设备的分辨率大小图标

ios - 为什么异步加载图像需要很长时间?

ios - MFMailComposeViewController 困惑

HTML 视频无法在苹果设备(Chrome/Safari/等)上运行

swift - Superfeedr 无法访问 Parse.com 回调 URL

swift - 创建 DispatchQueue Swift3 的方法有什么不同

ios - 使用Swift在IOS开发中导航栏覆盖音乐播放器

javascript - 将 <IMG> 转换为 base64 Javascript