将 UIImage 从 ImagePicker 解包到 Firebase.Storage 时致命
fatal when unwrapping UIImage from ImagePicker to Firebase.Storage
尝试从 UIImagePicker 创建存储在 Firebase 中的图像,在接收时抛出致命错误,因为 ImageData 为 nil。图像选择器 return 怎么可能为零?
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion:nil)
guard let uid = Auth.auth().currentUser?.uid else { return }
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
let imageData = UIImageJPEGRepresentation(image, 0.8)
let imagePath = "\(uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000)).jpg"
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(imagePath)
.putData(imageData!, metadata: metadata) { [weak self] (metadata, error) in
if let error = error {
print("Error uploading: \(error)")
return
}
}
你能删除这一行吗,
picker.dismiss(animated: true, completion:nil)
并将其放在 imagePickerController 方法的末尾而不是开头。您似乎过早地关闭了选择器,结果图像变成了 nil。
尝试从 UIImagePicker 创建存储在 Firebase 中的图像,在接收时抛出致命错误,因为 ImageData 为 nil。图像选择器 return 怎么可能为零?
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion:nil)
guard let uid = Auth.auth().currentUser?.uid else { return }
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
let imageData = UIImageJPEGRepresentation(image, 0.8)
let imagePath = "\(uid)/\(Int(Date.timeIntervalSinceReferenceDate * 1000)).jpg"
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(imagePath)
.putData(imageData!, metadata: metadata) { [weak self] (metadata, error) in
if let error = error {
print("Error uploading: \(error)")
return
}
}
你能删除这一行吗,
picker.dismiss(animated: true, completion:nil)
并将其放在 imagePickerController 方法的末尾而不是开头。您似乎过早地关闭了选择器,结果图像变成了 nil。