【问题标题】:How to fetch images by their uid's Swift Firebase?如何通过他们的 uid 的 Swift Firebase 获取图像?
【发布时间】:2017-08-29 01:14:21
【问题描述】:

我正在尝试显示当前用户发布的所有图像。正如您在表格视图中的图片中看到的那样,每次当前用户发布图像时,它都会自动为当前用户信息下的图像创建一个唯一的 id。

在此图像中,您将看到,当用户发布图像时,它会为其子图像创建一个名为 media 的节点。

这是我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellMe", for: indexPath) as! MyPosttTableViewCell


    FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
    // check if user has photo
    if snapshot.hasChild("media") != nil{
        print("found it")

     //set image locatin
    let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("media")"


    FIRStorage.storage().reference().child(filePath).data(withMaxSize: 10*1024*1024, completion: { (data, error) in

    let userPhoto = UIImage(data: data!)
    cell.Myimages.image = userPhoto

    })

    }
    else {
        print("nil")
        }
        })

        return cell


    }}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database firebase-storage


    【解决方案1】:

    如果您想将图像添加到特定 ID,那么将图像上传到 firebase 存储,获取绝对字符串中的 image.downloadurl,并将此值添加到对象的键“image”,然后将对象发布到火力数据库。

    【讨论】:

      【解决方案2】:

      用户节点中的媒体存储为 NSDictionary,而不是字符串。您将不得不遍历媒体节点以获取所有文件的路径。你可以做类似的事情

      //Create reference to media directly instead
      FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).child("media").observeSingleEvent(of: .value, with: { (snapshot) in
        if let media = snapshot.value as? NSDictionary {
      
          //Loop thorugh all the entries in your media node to get all the media paths
          for (_, value) in media {
            FIRStorage.storage().reference().child("media").child(value as! String)
      
            //get the file now that you have the reference
          }
        }
      })
      

      【讨论】:

      • 我想在 for 循环下写什么?细胞.myimages.image = ?
      猜你喜欢
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      • 2017-12-20
      • 2018-04-22
      • 2017-01-23
      • 2019-01-21
      相关资源
      最近更新 更多