将图像上传到 Firebase 存储

Posted

技术标签:

【中文标题】将图像上传到 Firebase 存储【英文标题】:Uploading image to Firebase Storage 【发布时间】:2016-09-21 00:37:26 【问题描述】:

我正在使用这个简单的代码。

let imageName = UUID().uuidString
        let storageRef = FIRStorage.storage().reference().child("Devices_Images").child("\(imageName).png")

       // let metaData = FIRStorageMetadata()
       // metaData.contentType = "image/png"

        if let uploadData = UIImagePNGRepresentation(self.ImageView.image!) 
            storageRef.put(uploadData, metadata: nil, completion:  (data, error) in
                if error != nil 
                    print(error)

                 else 
                    print("Image Uploaded Succesfully")
                    let profileImageUrl = data?.downloadURL()?.absoluteString
                    

我不断收到此错误:

[Generic] Creating an image format with an unknown type is an error

其实转换和图片类型都是png的!所以为什么我一直收到这个错误!

图像是从 alpum 或相机上传的,如下所示:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    let theInfo:NSDictionary = info as NSDictionary
    let img:UIImage = theInfo.object(forKey: UIImagePickerControllerOriginalImage) as! UIImage
    ImageView.image = img
    self.dismiss(animated: true, completion: nil)


@IBAction func AddPictureBtnAction(_ sender: AnyObject) 
    // addPictureBtnAtion.enabled = false
    let alertController : UIAlertController = UIAlertController(title: "أضف جهازًا", message: "التقط صورة من الكاميرا أو اختر من الألبوم", preferredStyle: .actionSheet)

    let cameraAction : UIAlertAction = UIAlertAction(title: "صورة من الكاميرا", style: .default, handler: (cameraAction) in
        print("camera Selected...")

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) == true 

            self.imagePicker.sourceType = .camera
            self.present()

        else
            self.present(self.showAlert("عذرًا", Message: "الكاميرا ليست متاحة في هذا الجهاز أو تم منع الوصول لها!"), animated: true, completion: nil)

        

    )

    let libraryAction : UIAlertAction = UIAlertAction(title: "صورة من الألبوم", style: .default, handler: (libraryAction) in

        print("Photo library selected....")

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) == true 

            self.imagePicker.sourceType = .photoLibrary
            self.present()

        else

            self.present(self.showAlert("عذرًا", Message: "ألبوم الصور ليس متاحًا في هذا الجهاز أو تم منع الوصول له!"), animated: true, completion: nil)
        
    )

    let cancelAction : UIAlertAction = UIAlertAction(title: "إلغاء", style: .cancel , handler: (cancelActn) in
        print("Cancel action was pressed")
    )

    alertController.addAction(cameraAction)

    alertController.addAction(libraryAction)

    alertController.addAction(cancelAction)

    alertController.popoverPresentationController?.sourceView = view
    alertController.popoverPresentationController?.sourceRect = view.frame

    self.present(alertController, animated: true, completion: nil)



func present()
    self.present(imagePicker, animated: true, completion: nil)



/*   func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
 print("info of the pic reached :\(info) ")
 self.imagePicker.dismissViewControllerAnimated(true, completion: nil)

  */

//Show Alert
func showAlert(_ Title : String!, Message : String!)  -> UIAlertController 

    let alertController : UIAlertController = UIAlertController(title: Title, message: Message, preferredStyle: .alert)
    let okAction : UIAlertAction = UIAlertAction(title: "Ok", style: .default)  (alert) in
        print("User pressed ok function")

    
    alertController.addAction(okAction)
    alertController.popoverPresentationController?.sourceView = view
    alertController.popoverPresentationController?.sourceRect = view.frame
    return alertController

【问题讨论】:

你的 self.ImageView.image 有问题! 去掉你孩子身上的 png("(imageName).png") 【参考方案1】:

在 imagePicker 方法的参数中添加“_”。

func imagePickerController(_ picker: UIImagePickerController ...

然后上传:-

   //if let uploadData = UIImagePNGRepresentation(UIImage(cgImage: self.imageView.image! as! CGImage, scale: 1.0, orientation: .up)) as? NSData 
   if let uploadData = UIImagePNGRepresentation(self.ImageView.image!) as? NSData   



         storageRef.put(uploadData!, metadata: nil, completion:  (metadata, error) in
        if error != nil 
                print(error)

             else 
                print("Image Uploaded Succesfully")
                let profileImageUrl = data?.downloadURL()?.absoluteString
           
    )
  

【讨论】:

你应该在 profileImageUrl 中使用 metaData 而不是 data 仍在获取:[Generic] Creating an image format with an unknown type is an error 函数里居然有下划线! 看来linke问题不在于升级方法!但具有从相机或相册上传的功能! 从相机拍照后出现此错误:2016-09-21 14:52:33.570382 Jabeerah[3735:1722640] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2016-09-21 14:52:33.581981 Jabeerah[3735:1722640] [MC] Reading from public effective user settings.

以上是关于将图像上传到 Firebase 存储的主要内容,如果未能解决你的问题,请参考以下文章

Flutter - 将图像上传到 Firebase 存储

将多个图像的异步图像上传到firebase存储

Firebase 存储 - 将图像上传到 Firebase 存储并在实时数据库中创建指向该图像的链接

如何将图像上传到firebase存储然后将图像url存储在firestore中?

成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL

Flutter - 将图像上传到 Firebase 存储