Swift 从Firebase下载图像
请帮帮我。解释如何在单元格中设置图像。在我的数据库中,我有:title、description和imageURL(来自Firebase存储的url)。你能给我写个代码解释一下吗Swift 从Firebase下载图像,swift,firebase-realtime-database,uicollectionview,uicollectionviewcell,firebase-storage,Swift,Firebase Realtime Database,Uicollectionview,Uicollectionviewcell,Firebase Storage,请帮帮我。解释如何在单元格中设置图像。在我的数据库中,我有:title、description和imageURL(来自Firebase存储的url)。你能给我写个代码解释一下吗 class TrainingProgram { var description = "" var title = "" var imageURL = "" init(description: String, title: String, imageURL: String) {
class TrainingProgram
{
var description = ""
var title = ""
var imageURL = ""
init(description: String, title: String, imageURL: String) {
self.description = description
self.title = title
self.imageURL = imageURL
}
函数从firebase获取数据
func fetchPrograms() {
Database.database().reference().child("programs").observe(.childAdded) { (snapshot) in
if let dict = snapshot.value as? [String: AnyObject] {
let newTitle = dict["title"] as! String
let newDescription = dict["description"] as! String
let newImageURL = dict["imageURL"] as! String
let trainingCell = TrainingProgram(description: newDescription, title: newTitle, imageURL: newImageURL)
self.trainingPrograms.append(trainingCell)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
}
这就是我设置单元格标题和描述的方式
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as! TrainingProgramCollectionViewCell
//cell.featuredImageView.setImage(from: indexPath.item)
cell.titleLabel.text = trainingPrograms[indexPath.item].title
cell.descriptionLabel.text = trainingPrograms[indexPath.item].description
我需要在函数中编写什么来接收数据以获取图像,以及如何在单元格中设置图像
从此链接安装SDWebImage pod 在CollectionView
cellForRow
方法中
var images_list = [String]()
var imagesArray = [URL]()
images_list.append(itemModel[indexPath.row]. imageURL)
let storage = Storage.storage().reference()
for x in images_list{
print(x)
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesArray.append(url!)
}
if let x = images_list.last{
cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
}
}
}
完整代码
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as! TrainingProgramCollectionViewCell
//cell.featuredImageView.setImage(from: indexPath.item)
cell.titleLabel.text = trainingPrograms[indexPath.item].title
cell.descriptionLabel.text = trainingPrograms[indexPath.item].description
var images_list = [String]()
var imagesArray = [URL]()
for x in images_list{
print(x)
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesArray.append(url!)
}
if let x = images_list.last{
cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
}
}
}
}
下载单幅图像时请尝试此方法,只需从firebase获取您的imageUrl即可
let x: String = "yourImageUrl"
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesArray.append(url!)
}
if let x = images_list.last{
cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
}
}
是否要在tableView单元格上显示图像?@wings是的。我不明白如何在我的电脑中显示firebase的图像cells@wings你能给我写一个代码,它将从FirebaseDatabase获取url(DB有来自存储的url)并显示在单元格上吗?@我添加的DBComments结构不用于扩展讨论;这段对话已经结束。