我正在尝试将图像上传到Firebase存储,而尝试上传数据时,我的应用程序崩溃了。 这是我得到的错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'
这是我的代码(用户单击上传按钮后,调用uploadPostImage将其上传到Firebase存储):
@objc func addBtnTapped(){
//Checking if the user chose photo, so user's won't upload photos with the placeholder image.
if userChoseImage == true{
let userID : String = (Auth.auth().currentUser?.uid)!
let postUUID = UUID().uuidString
guard let postTitle = self.titleTextField.text else { return }
uploadPostImage { url in
self.ref.child("posts").child(postUUID).setValue(["title": postTitle,"imageUrl": "http://www.test.com" ,"numberOfLikes": 0, "numberOfDislikes": 0, "postCreator": userID])
}
}else{
let alert = UIAlertController(title: "Choose Image", message: "Please chose image by clicking on the image placeholder photo.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true)
}
}
这是uploadPostImage:
func uploadPostImage(completion: @escaping((_ url: String?)->())){
let storageRef = Storage.storage().reference()
guard let uploadData = self.postImage.image?.jpegData(compressionQuality: 0.8) else { return }
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
//I think the crash happens here
storageRef.putData(uploadData, metadata: metaData) { metaData, error in
if error == nil && metaData != nil{
if let url = metaData?.downloadURL()?.absoluteString{
completion(url)
}else{
completion(nil)
}
}else{
}
}
}
我以为,也许我得到的URL是零,但是我试图在其中扔一个URL,但是它仍然崩溃,我在uploadPostImage中设置了一个断点,但似乎没有什么是零。 我真的不知道发生了什么。