[英]How to fetch images by their uid's Swift Firebase?
I am trying to show all images that the current user posts. 我正在尝试显示当前用户发布的所有图像。 As you can see in the picture in a table view, every time the current user post an image it automatically creates a unique id for the image under the current user information.
正如您在表格视图的图片中所看到的,当前用户每次发布图像时,都会自动在当前用户信息下为该图像创建唯一ID。
In this images, You will see that when the users post an image it create a node called media for the image with their children. 在此图像中,您将看到,当用户发布图像时,它将与孩子一起为该图像创建一个称为媒体的节点。
Here's my code: 这是我的代码:
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
}}
The media in your users node is stored as a NSDictionary, not as a String. 用户节点中的媒体存储为NSDictionary,而不是字符串。 You will have to loop through the media node to get the paths of all your files.
您将必须遍历媒体节点以获取所有文件的路径。 You can do something like
你可以做类似的事情
//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
}
}
})
如果您想将图像添加到特定的ID,然后将图像上传到Firebase存储,则以绝对字符串形式获取image.downloadurl,并将此值添加到对象的“ image”键中,然后将该对象发布到Firebase数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.