ios - Firebase 存储图像未在 Tableview 中下载

标签 ios swift image firebase firebase-storage

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

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

关于ios - Firebase 存储图像未在 Tableview 中下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44095299/

相关文章:

ios - 在 iOS 的 Realm 中添加数据的问题

android - Flutter - 试图从 reducer 获取小部件上下文的脚手架

ios - 从框架加载初始 View

ios - NSUTF8StringEncoding 结果编码字符串为 'Optional' 字符串

arrays - 具有特定类型的对象数组

html - 关于如何使用 css 向图像添加透明渐变的任何想法?

objective-c - 在 Objective C 中创建私有(private) setter 的警告

ios - 不断调用方法,直到 View 被取消

php - 无限图像上传到服务器和名称、类别等和多个图像路径(1 行)到数据库

javascript - 突出显示图像区域 - 单击链接时