繁体   English   中英

从 firebase 存储 SWIFT 中检索照片

[英]Retrieve photos from firebase storage SWIFT

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

我有一个使用原型单元格、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] = []

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

  1. 首先,您需要修复您的 model 以便它可以帮助您。 像这样将存储桶名称添加到 model 中:
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 Model 匹配,并且您的存储根目录中也有与所有photoKey匹配的图像。 顺便说一句, Labels不是一个很好的 model 名称,我只是为了保持一致而使用它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM