iOS Firebase-获取照片的URL(无法解析的标识符)的问题

时间:2018-11-21 19:11:09

标签: ios swift firebase

我正在尝试获取用户选择的照片的URL,然后将其发送到Firebase。我在最后一行收到此错误: 使用未解决的标识符“ profileImageUrl”;你是说'profileImage'吗?

@IBAction func signUpBtn(_ sender: Any) {
    Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
        if error != nil {
            print("error")
            return
        }


        let storageRef = Storage.storage().reference(forURL: "gs://justforpractice.appspot.com").child("profile_image").child(userID)
        if let profileImg = self.selectedImage, let imageData = self.selectedImage?.jpegData(compressionQuality: 0.1){
            storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    return
                }


                storageRef.downloadURL { (url, error) in
                    guard let profileImageUrl = url else {
                        // An error occurred!
                        return
                    }
                }

                let ref = Database.database().reference()
                let usersReference = ref.child("users")
                let newUserReference = usersReference.child(userID)
                newUserReference.setValue(["username": self.usernameTextField.text!, "email": self.emailTextField.text!, "profileImageUrl": profileImageUrl])

            })
        }
    })
}

1 个答案:

答案 0 :(得分:1)

您应将代码放在storageRef.downloadUrl闭包中,以便可以访问展开的url

storageRef.downloadURL { (url, error) in
    guard let profileImageUrl = url else {
        // An error occurred!
        return
    }

    let ref = Database.database().reference()
    let usersReference = ref.child("users")
    let newUserReference = usersReference.child(userID)
    newUserReference.setValue(["username": self.usernameTextField.text!, "email": self.emailTextField.text!, "profileImageUrl": profileImageUrl])
}