我想将Jpeg类型的图像上传到Firebase存储
以下是上传到Firebase存储的功能
func uploadImageToFirebase (data: NSData){
let StorageRef = Storage.storage().reference().child("User Products")
let uploadMetadata = StorageMetadata()
uploadMetadata.contentType = "image/jpeg"
StorageRef.putData(data as Data, metadata: uploadMetadata) { (metadata, error) in
if (error != nil){
print("I have an error")
}else {
print("upload metadata \(String(describing: metadata))")
}
}
这里我有一个选择器控制器函数,该函数位于另一个名为CamViewController
在这里,我从Mainviewcontroller
类调用该函数
var discover = MainViewController()
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
imageCam.image = image
discover.uploadImageToFirebase(data: image)
picker.dismiss(animated: true, completion: nil)
}
我得到了错误:
无法将类型
UIImage?
的值转换为预期的参数类型NSData
答案 0 :(得分:0)
您需要
func uploadImageToFirebase (_ image:UIImage){
gurad let data = image.pngData() else { return }
..... OR
guard let data = image.jpegData(compressionQuality: 0.5) else { return }
}
discover.uploadImageToFirebase(image)
错误很明显,参数类型为NSData
,所以您不能传递UIImage