Ios 更新照片后Firebase Swift崩溃
我不熟悉编码和firebase。我能够让我的图像选择器调用照片,但当用户使用图像选择器更新照片时,它会导致应用程序崩溃,但它能够使用新照片更新我的firebase数据库和firebase存储 这是更新照片后的代码和错误消息Ios 更新照片后Firebase Swift崩溃,ios,swift,firebase,firebase-realtime-database,firebase-storage,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Storage,我不熟悉编码和firebase。我能够让我的图像选择器调用照片,但当用户使用图像选择器更新照片时,它会导致应用程序崩溃,但它能够使用新照片更新我的firebase数据库和firebase存储 这是更新照片后的代码和错误消息 func loadProfileImage() { Database.database().reference().child("users").child(Auth.auth().currentUser!.uid).child("profileImag
func loadProfileImage() {
Database.database().reference().child("users").child(Auth.auth().currentUser!.uid).child("profileImageUrl").observe(.value, with: { (snapshot) in
// Get download URL from snapshot
let downloadURL = snapshot.value as! String
// Create a storage reference from the URL
let storageRefPF = Storage.storage().reference(forURL: downloadURL)
// Download the data, assuming a max size of 1MB (you can change this as necessary)
storageRefPF.getData(maxSize: 1 * 1024 * 1024) { (data,error) -> Void in
// Create a UIImage, add it to the array
self.ProfileImageF.image = UIImage(data: data!)
print(snapshot)
//print(PIUvalue as Any)
}
})
}
线程1:致命错误:在展开可选值时意外发现nil
我真的需要帮助。崩溃原因: 您的应用程序正在崩溃,因为您正在完成块中强制展开数据。强制展开
nil
值将使应用程序崩溃
有关这方面的更多信息:
&
修复:
替换
self.ProfileImageF.image = UIImage(data: data!)
与
这将修复您的崩溃。如果
data
仍为nil
且图像未显示,我建议您检查firebase存储规则并确保您拥有正确的权限 真的很感激!
if let imgData = data {
self.ProfileImageF.image = UIImage(data: imgData)
}