【问题标题】:Retrieve photos from firebase storage SWIFT从 Firebase 存储 SWIFT 检索照片
【发布时间】:2021-07-04 16:52:55
【问题描述】:

我从昨天早上开始就遇到了问题,但我不知道如何解决这个问题。

我有一个使用原型单元格、2 个标签和 1 张照片的表格视图。对于我使用 Firestore 的标签和图片 Firebase 存储。

问题是我知道如何从我的 firebase 存储中检索照片的唯一方法是这段代码

 
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let ref = storageRef.child("Mancare/Mancare3.jpg")
        testImage.sd_setImage(with: ref)

我想将照片检索到我的表格视图中,但我不知道如何才能做到这一点。

这就是我使用 Firestore 检索标签的方法。我将只粘贴代码的必要部分:

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


 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableViewTest.dequeueReusableCell(withIdentifier: "CountryTableViewCell", for: indexPath) as! CountryTableViewCell
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let ref = storageRef.child("Mancare/Mancare3.jpg")
        
        let label = labels[indexPath.row]
        cell.labelTest.text = label.firstLabel
        cell.labelLaba.text = label.secondLabel
      
        
        
        return cell
        
        
    }

func getDatabaseRecords() {
        
        let db = Firestore.firestore()
        labels = []  //  Empty the array
        db.collection("labels").getDocuments { (snapshot, error) in
            if let error = error {
                print(error)
                return
            } else {
                for document in snapshot!.documents {
                    let data = document.data()
                    let newEntry = Labels(
                        firstLabel: data["firstLabel"] as! String,
                        secondLabel: data["secondLabel"] as! String)
                        
                        
                        
                    self.labels
                        .append(newEntry)
                }
            }
            DispatchQueue.main.async {
                self.tableViewTest.reloadData()
            }
        }
    }





这就是我声明标签的方式:

struct Labels {
    let firstLabel: String
    let secondLabel: String
   
  
}

 var labels: [Labels] = []

如果有人能帮助我,我将永远感激不尽。谢谢

【问题讨论】:

    标签: ios swift firebase firebase-storage


    【解决方案1】:
    1. 首先,您需要修复您的模型,以便它可以帮助您。像这样将存储桶名称添加到模型中:
    Struct Labels {
        let firstLabel: String
        let secondLabel: String
        let photoKey: String // This will store the bucket name for this `Labels`
    } 
    
    1. 现在在您的getDatabaseRecords 更改:
    let newEntry = Labels(firstLabel: data["firstLabel"] as! String,
                          secondLabel: data["secondLabel"] as! String),
                          photoKey: data["photoKey"] as! String) // Added Line
    
    1. 然后在cellForRow:
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableViewTest.dequeueReusableCell(withIdentifier: "CountryTableViewCell", for: indexPath) as! CountryTableViewCell
        let label = labels[indexPath.row]
        let storageRef = Storage.storage().reference()
        let photoRef = storageRef.child(label.photoKey)
            
        cell.labelTest.text = label.firstLabel
        cell.labelLaba.text = label.secondLabel
        cell.imageView.sd_setImage(with: photoRef) // Assuming the image view in your cell is named this
          
        return cell
    }
    
    1. 最后,确保您的文档结构与 firebase 控制台中的新 Labels 模型相匹配,并且您的存储根目录中也有与所有 photoKeys 匹配的图像。顺便说一句,Labels 不是一个很好的型号名称,我只是为了保持一致而使用它

    【讨论】:

    • 效果很好,非常感谢。题外话,另一个与此相关的问题,当我运行应用程序时,我只有一行数据。我怎样才能用不同的图片、标签等制作不同的行:)。 imgur.com/U2GemAg - 表格视图
    • 您只需像添加第一个一样将它们添加到数据库中,然后也为它们添加照片。它应该会自动填充到您的 TableView 中
    • 所以,我只需要在我的收藏中添加更多文档。有效,谢谢。祝你有美好的一天:)
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 2012-09-15
    • 2017-05-09
    • 1970-01-01
    • 2017-01-16
    • 2020-10-15
    • 2016-12-27
    • 2017-01-14
    相关资源
    最近更新 更多