简体   繁体   English

从 firebase 存储 SWIFT 中检索照片

[英]Retrieve photos from firebase storage SWIFT

I have a problem since yesterday morning but I can't figure it out how can I resolve this issue.从昨天早上开始我就遇到了问题,但我不知道如何解决这个问题。

I'm having a table view which is using prototype cells, 2 labels and 1 photo.我有一个使用原型单元格、2 个标签和 1 张照片的表格视图。 For the labels I used Firestore and for the picture firebase storage.对于我使用 Firestore 的标签和图片 firebase 存储。

The problems is that the only way I know how to retrieve photos from my firebase storage is this code问题是我知道如何从我的 firebase 存储中检索照片的唯一方法是这段代码

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

I want to retrieve the photos into my table view, but I do not know how can I can accomplish that.我想将照片检索到我的表格视图中,但我不知道如何才能做到这一点。

This is what im using for retrieving the labels with Firestore.这就是我使用 Firestore 检索标签的方法。 I'll paste only the necessary parts of the code:我将只粘贴代码的必要部分:

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()
            }
        }
    }





This is how I declared the labels:这就是我声明标签的方式:

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

 var labels: [Labels] = []

If someone can help me, ill be forever grateful.如果有人可以帮助我,我将永远感激不尽。 Thanks谢谢

  1. First, you need to fix your model so it can help you.首先,您需要修复您的 model 以便它可以帮助您。 Add the bucket name to the model like this:像这样将存储桶名称添加到 model 中:
Struct Labels {
    let firstLabel: String
    let secondLabel: String
    let photoKey: String // This will store the bucket name for this `Labels`
} 
  1. Now in your getDatabaseRecords change:现在在您的getDatabaseRecords更改中:
let newEntry = Labels(firstLabel: data["firstLabel"] as! String,
                      secondLabel: data["secondLabel"] as! String),
                      photoKey: data["photoKey"] as! String) // Added Line
  1. Then in cellForRow :然后在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. Last, make sure your document structure matches the new Labels Model in the firebase console, and you have images as well in the root of your storage that match with all the photoKey s.最后,确保您的文档结构与 firebase 控制台中的新Labels Model 匹配,并且您的存储根目录中也有与所有photoKey匹配的图像。 Btw, Labels is not a very good model name, I just went with it for consistency顺便说一句, Labels不是一个很好的 model 名称,我只是为了保持一致而使用它

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

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