我正在尝试显示当前用户发布的所有图像。正如您在表格 View 中的图片中看到的,每次当前用户发布图像时,它都会在当前用户信息下自动为该图像创建一个唯一的 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
}}
最佳答案
如果您想将图像添加到特定 ID,则将图像上传到 Firebase 存储,获取绝对字符串中的 image.downloadurl 并将该值添加到对象的“image”键,然后将对象发布到火力数据库。
关于ios - 如何通过其 uid 的 Swift Firebase 获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197030/