0

I want to turn the URL (downloadURL) into an actual image that will go in my table view cell. The URL that it will retrieve is a link to download the image. This is what I have so far. Thanks!

override func viewDidLoad() { super.viewDidLoad()

    let ref = FIRDatabase.database().reference().child("Posts")

    ref.observeSingleEvent(of: .value, with: { snapshot in

        print(snapshot.childrenCount)

        for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {

            guard let value = rest.value as? Dictionary<String,Any> else { continue }

            guard let  title = value["Title"] as? String else { continue }

            guard let  date = value["Date"] as? String else { continue }

            guard let  author = value["Author"] as? String else { continue }

            guard let  article = value["Article"] as? String else { continue }

            guard let  downloadURL = value["DownloadURL"] as? String else { continue }

            let post = postStruct(title: title, date: date, author: author, article: article, downloadURL: downloadURL)

            self.posts.append(post)

        }

        self.posts = self.posts.reversed(); self.tableView.reloadData()

    })


}






override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = posts[indexPath.row].title
    return cell!
}

}

Riccardo
  • 289
  • 1
  • 4
  • 22

1 Answers1

0

A popular library to use is -

https://github.com/rs/SDWebImage

This will handle the image loading for you using -

imageView.sd_setImage(with: URL(string: downloadURL), placeholderImage: UIImage(named: "placeholder.png"))

You can add in your own placeholder or not use any. I believe it will also handle caching for you too.

To learn how to setup CocoaPods this library follow -

https://guides.cocoapods.org/using/getting-started.html

In your podfile add -

pod 'SDWebImage', '~>3.8'