如何使用 SDWebImage 缓存 swift 中的图像?

How to cache the image in swift using SDWebImage?

我正在从 firebase 存储中获取图像并想使用 SDWebImage 缓存它,但在我的 ViewController 中它会继续下载。请指导我如何缓存图像

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! demoCell
        cell.textDemo.text = images[indexPath.row]

        var images_list = [String]()
        images_list.append(images[indexPath.row])
        images_list.append(images[indexPath.row] + "1")
        let storage = Storage.storage().reference()
        var imagesarray = [URL]()


        for x in images_list{
        let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else if let downloadURL = url?.absoluteString{
                cell.imageDemo.image = SDImageCache.shared().imageFromCache(forKey: downloadURL)
                print("Image Cached")
            }
            else{
                cell.imageDemo.sd_setImage(with: url!, completed: nil)
            }
        }
    }
    return cell
    }

而不是写作:

cell.imageDemo.image = SDImageCache.shared().imageFromCache(forKey: downloadURL)

试试这个:

cell.imageDemo.sd_setImage(with: URL(string: downloadURL), placeholderImage: UIImage(named: "Your Default Image"))

SDWebImage 将在这里处理缓存部分。

这是我对问题的解决方案

var value : Any?
var vc = ViewController()
var images = [String]()
var downloads_array = [URL]()


override func viewDidLoad() {
    super.viewDidLoad()
    getImageNames()
    downloadImages()

}

func downloadImages(){
    let storage = Storage.storage().reference()

    for x in images{
    let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                self.downloads_array.append(url!)
                self.tableView.reloadData()
            }
        }
    }
}

func getImageNames(){
    images = vc.images
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return downloads_array.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! demoCell
    cell.textDemo.text = images[indexPath.row]

    if let downloadURL = SDImageCache.shared().imageFromCache(forKey: downloads_array[indexPath.row].absoluteString){
        cell.imageDemo.image = downloadURL
    }
    else{
        cell.imageDemo.sd_setImage(with: downloads_array[indexPath.row], completed: nil)
    }



    return cell
}