Ios 如何将URL作为图像加载?
我想把URL(downloadURL)转换成一个实际的图像,它将出现在我的表视图单元格中。它将检索的URL是下载图像的链接。这就是我目前所拥有的。谢谢 重写func viewDidLoad(){ super.viewDidLoad()Ios 如何将URL作为图像加载?,ios,swift,xcode,uitableview,firebase,Ios,Swift,Xcode,Uitableview,Firebase,我想把URL(downloadURL)转换成一个实际的图像,它将出现在我的表视图单元格中。它将检索的URL是下载图像的链接。这就是我目前所拥有的。谢谢 重写func viewDidLoad(){ super.viewDidLoad() let ref=FIRDatabase.database().reference().child(“Posts”) ref.observeSingleEvent(的值为:,其中:{snapshot in 打印(快照.childrenCount) 对于snapsho
let ref=FIRDatabase.database().reference().child(“Posts”)
ref.observeSingleEvent(的值为:,其中:{snapshot in
打印(快照.childrenCount)
对于snapshot.children.allObjects as![FIRDataSnapshot]中的rest{
guard let value=rest.value as?Dictionary else{continue}
guard let title=值[“title”]为字符串,否则{continue}
guard let date=值[“date”]为?字符串,否则{continue}
guard let author=value[“author”]as?String else{continue}
guard let article=value[“article”]as?String else{continue}
guard let downloadURL=值[“downloadURL”]为?字符串,否则{continue}
let post=postStruct(标题:标题,日期:日期,作者:作者,文章:文章,下载URL:downloadURL)
self.posts.append(post)
}
self.posts=self.posts.reversed();self.tableView.reloadData()
})
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回岗位数
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带标识符:“cell”)
设label1=单元格?.viewWithTag(1)为!UILabel
label1.text=posts[indexPath.row].title
返回单元!
}
}一个常用的库是- 这将使用以下命令为您处理图像加载:
imageView.sd\u setImage(带:URL(字符串:downloadURL)、占位符image:UIImage(名为:“placeholder.png”))
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!
}