2016-09-21 192 views
0

我正在使用此簡單代碼將圖片上傳到Firebase存儲。將圖片上傳到Firebase存儲

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 
} 
+0

出錯了self.ImageView.image! –

+0

擺脫你的孩子的PNG(「\(imageName).png」) –

回答

0

添加「_」在你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 
     } 
    }) 
    } 
+0

你應該在profileImageUrl中使用metaData而不是數據 – Mariah

+0

仍然得到:''[Generic]創建一個未知類型的圖像格式是一個錯誤' – Mariah

+0

實際上功能中有下劃線! – Mariah

相關問題