【问题标题】:How to get imageUrl from firebase storage and store in firebase database in Xcode 10 swift 4.2如何从 Firebase 存储中获取 imageUrl 并将其存储在 Xcode 10 swift 4.2 中的 firebase 数据库中
【发布时间】:2019-03-16 04:19:11
【问题描述】:

这是我正在使用的代码

@objc func handleSignUp() {
        guard let email = emailTextField.text, email.characters.count > 0 else { return }
        guard let username = usernameTextField.text, username.characters.count > 0 else { return }
        guard let password = passwordTextField.text, password.characters.count > 0 else { return }

    Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
         if let err = error {
            print("Failed to create user:", err)
            return
        }
        print("Successfully created user:", user?.user.uid)
        //            guard let image = self.plusPhotoButton.imageView?.image else { return }
        //            guard let uploadData = UIImageJPEGRepresentation(image, 0.3) else { return }
        guard let uploadData = self.plusPhotoButton.imageView?.image?.jpegData(compressionQuality: 0.3) else{return}

        let filename = NSUUID().uuidString
        Storage.storage().reference().child("profile_images").child(filename).putData(uploadData, metadata: nil, completion: { (metadata, err) in
             if let err = err {
                print("Failed to upload profile image:", err)
                return
            }
//this code isn't correct for swift4.2
//guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else    { return } 


            func getDownloadURL(from path: String, completion: @escaping (URL?, Error?) -> Void) {
                let storageReference = Storage.storage().reference(forURL: "gs://instagramfirebase-60be5.appspot.com")

                let storageRef = storageReference.child(path).downloadURL(completion: completion)
            }
            print("Successfully uploaded profile image")//,profileImageUrl

            guard let uid = user?.user.uid else { return }
            let dictionaryValues = ["username": username] as [String : Any]//, "profileImageUrl": profileImageUrl
            let values = [uid: dictionaryValues]

            Database.database().reference().child("users").updateChildValues(values, withCompletionBlock: { (err, ref) in

                if let err = err {
                    print("Failed to save user info into db:", err)
                    return
                }

                print("Successfully saved user info to db")
            })
        })
    })
}

截图如下:

我对如何从 firebase 存储中获取 imageUrl 并将其存储在 Xcode 10 swift 4.2 中的 firebase 数据库中感到困惑

请帮忙,感谢您的宝贵时间。

【问题讨论】:

    标签: swift xcode firebase xcode10 swift4.2


    【解决方案1】:

    您可以通过在图片上传的闭包中使用以下代码轻松获取下载网址:

    storageRef.downloadURL(completion: { (url, error) in
        if let err = error{
           // error happened - implement code to handle it
           print(err)
        } else {
           // no error happened; so just continue with your code
           print(url?.absoluteString) // this is the actual download url - the absolute string
        }
    

    请注意:不要单独存储 downloadURL,因为 Firebase 可以更改 downloadURL 的令牌,我建议始终抓住完整的存储路径。

    希望能帮到你。

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2019-12-20
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多