ios - 如何通过其 uid 的 Swift Firebase 获取图像?

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

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

screenshot

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

screenshot

这是我的代码:

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/

相关文章:

android - UIBezierPath 到 NSData 到服务器到 Android

ios - 为什么我不能将收到的(通过 alamofire 调用) token 放入我的 swift 应用程序的钥匙串(keychain)中?

swift - Swift 中的绝对 UTC 偏移量

firebase - 重新部署 Cloud Functions 如何影响事件队列

ios - 从 XCode 运行时找不到钥匙串(keychain)值

ios - 如何获取适合特定矩形的 NSAttributedString 子字符串?

Swift:在带有委托(delegate)的模态视图上显示导航栏/顶部栏

swift - 以毫秒为单位的 timeIntervalSince1970 使用什么类型?

firebase - 从firebase客户端sdk获取user_pseudo_id?

javascript - 如何在 Firefox 控制台上启用显示 Firebase 的 DocumentReference