Firebase 存储图像未在 Tableview 中下载

Firebase Storage Image Not Downloading in Tableview

Firebase 存储图像未在 Tableview 中下载。如果我替换行 let tempImageRef = storage.child("myFiles/sample.jpg"),它会显示单个图像。但是如果试图抓取 'myFiles' 内的所有图像,它就不起作用。请帮忙

func fetchPhotos()
    {
        //let database = FIRDatabase.database().reference()
        let storage = FIRStorage.storage().reference(forURL: "gs://fir-test-bafca.appspot.com")
        let tempImageRef = storage.child("myFiles/")

        tempImageRef.data(withMaxSize: (1*1024*1024)) { (data, error) in
            if error != nil{
                print(error!)
            }else{
                print(data!)

                let pic = UIImage(data: data!)
                self.picArray.append(pic!)

                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
}

您正在引用整个文件夹 (myFiles/)。那行不通的。您需要单独引用每张图片。

一种方法是在上传图像时将图像元数据写入实时数据库。然后读取元数据(更具体地说是路径),然后以这种方式引用图像。

如果你有3张图片要上传和存储,你可以这样写元数据:

/images
  /image1key
    /metdata
      /path
  /image2key
    /metdata
     /path
  /image3key
    /metdata
      /path

然后您可以像这样查询数据库中的图片路径

let ref = Firebase.database().ref("images")
ref.observe(.value, withCompletion: { snapshot in
  if let values = snapshot.value as? [String : Any] {
    if let metadata = values["metadata"] as? [String: Any] {
      let imagePath = metadata["path"]
      //Now download your image from storage.
    }
  }
})

这不是最干净的代码,您绝对可以改进,但它会起作用:)