如何使用SDWebImage快速缓存图像?

时间:2018-09-17 04:05:10

标签: swift caching 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
    }

2 个答案:

答案 0 :(得分:1)

与其写作:

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

尝试一下:

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

SDWebImage将在此处处理缓存部分。

答案 1 :(得分:0)

这是我解决问题的方法

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
}