创建firebase聊天演示:我想上传用户图片但是它给出了一个错误

时间:2017-10-03 06:11:25

标签: ios swift firebase swift4

发现扩展程序时遇到错误:

  

错误域= PlugInKit代码= 13"查询已取消"   UserInfo = {NSLocalizedDescription =查询已取消} 2017-10-03   11:08:38.585084 + 0530 NewChatDemo [858:20663] *终止应用程序   未被捕获的异常' NSInvalidArgumentException',原因:' *    - [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象中插入nil对象[1]

这是我的 Swift 4 代码:

@objc func handleRegister()
{
    guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text else{
        print("invalid form")

        return
    }

    Auth.auth().createUser(withEmail: email, password: password, completion: { (user: User? , error) in

        if error != nil
        {
            print(error)
            return
        }

        guard let uid = user?.uid else{
            return
        }

        //successfully authenticated user
        let storageRef = Storage.storage().reference()

        if let uploadData = UIImagePNGRepresentation(self.profileImageView.image!){
            storageRef.putData(uploadData, metadata: nil, completion: {(metadata, error) in
                if error != nil
                {
                    print(error)
                }
                else
                {
                    print(metadata)
                }
            })
        }

        let ref = Database.database().reference(fromURL: "https://newchatapp-470cb.firebaseio.com/")

        let uerReference = ref.child("users").child(uid)

        let values = ["name": name, "email": email]

        uerReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
            if err != nil
            {
                print(err)
                return
            }

            print("Successfully saved user in firebase db")
        })

    })
}

@objc func handleSelectProfileImageView()
{
    let picker = UIImagePickerController()
    picker.delegate = self
    picker.allowsEditing = true

    present(picker, animated: true, completion: nil)

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print(info)

    var selectedImageFromPicker: UIImage?

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage
    {
        selectedImageFromPicker = editedImage as! UIImage


    }else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage{

        selectedImageFromPicker = originalImage as! UIImage


    }

    dismiss(animated: true, completion: nil)

    if var selectedImage = selectedImageFromPicker
    {
        profileImageView.image = selectedImage
    }

}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    print("Cancel picker")


}

1 个答案:

答案 0 :(得分:1)

我认为这个错误是由于这一行:

let storageRef = Storage.storage().reference()

您需要提供要上传的文件的名称,以便您可以执行以下操作:

let storageRef = Storage.storage().reference().child("yourImageName.png")