尝试从UIImagePicker创建存储在Firebase中的图像,因为ImageData为零而收到致命信息。图像选择器如何返回零?
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
}
}
答案 0 :(得分:1)
你可以删除这一行吗,
picker.dismiss(animated: true, completion:nil)
并将其放在imagePickerController方法的末尾,而不是在开头。看起来你过早地解雇了选择器,因此图像变为零。