我想将URL(downloadURL)转换为将放在我的表格视图单元格中的实际图像。它将检索的URL是下载图像的链接。这就是我到目前为止所拥有的。谢谢!
覆盖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!
}
}
答案 0 :(得分:0)
一个流行的图书馆是 -
https://github.com/rs/SDWebImage
这将使用 -
为您处理图像加载
imageView.sd_setImage(with: URL(string: downloadURL), placeholderImage: UIImage(named: "placeholder.png"))
您可以添加自己的占位符或不使用任何占位符。我相信它也会为你处理缓存。
要了解如何设置CocoaPods这个库如下 -
https://guides.cocoapods.org/using/getting-started.html
在您的podfile中添加 -
pod'SDWebImage','〜&gt; 3.8'