【问题标题】:I upload a picture successfully to the firebase storage but the download url is not stored in the database我成功将图片上传到 firebase 存储,但下载 url 未存储在数据库中
【发布时间】:2019-12-10 14:09:38
【问题描述】:

我正在尝试将存储的图像的 url 保存在数据库中,但是没有成功。它没有在数据库中显示任何 url,因为它似乎跳过了以下 saveButton 函数中的说明。

@IBAction func saveButtonClicked(_ sender: Any) {
    let storageRef = Storage.storage().reference().child("profileImages").child(id!)

    if let uploadData = profilePicture.image?.pngData(){

        storageRef.putData(uploadData, metadata: nil) { (metadata, error) in
            if error != nil{
                print(error)
                return
            }

            storageRef.downloadURL(completion: { (url, error) in
                if let downUrl = url {

                    Database.database().reference().child("Users").child(self.id!).child("profileImageUrl").setValue(downUrl)
                }
            })
        }



    }else{
        Database.database().reference().child("Users").child(self.id!).child("profileImageUrl").setValue("default")

    }

    self.performSegue(withIdentifier: "goToMainPage", sender: self)

}

}

【问题讨论】:

  • 您必须在终端中收到警告?确保 downUrl 是一个字符串。所以检查 downUrl.stringValue 是否有效。如果这不起作用,请为 setValue 设置完成句柄以捕获错误消息。
  • "它似乎跳过了指令" 听起来您正在调试器中单步执行代码。您调用的某些方法是异步的,这意味着您将无法单步执行它们。而是设置断点(例如在if error != nil{if let downUrl = url { 上),然后在调试器中再次运行它以查看断点是否被命中。

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


【解决方案1】:

尝试替换

storageRef.downloadURL(completion: { (url, error) in
                if let downUrl = url {

                    Database.database().reference().child("Users").child(self.id!).child("profileImageUrl").setValue(downUrl)
                }
            })

通过

let pat = (metadata?.downloadURL()?.absoluteString.description)
let link = pat! //Link of image
Database.database().reference().child("Users").child(self.id!).child("profileImageUrl").setValue(link)

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 2018-02-10
    • 1970-01-01
    • 2016-12-24
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多