2016-12-24 143 views
2

我已經建立並在UITableView數據中顯示圖書的應用程序。所有作品都完美無瑕,我唯一需要的是書中的照片。要上傳和檢索照片,我使用Firebase,但我不知道如何處理照片。從Firebase上傳和檢索照片

這是我已經實現:

@IBAction func ButtonScatta(_ sender: UIButton) { 

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.camera; 
     imagePicker.allowsEditing = false 
     self.present(imagePicker, animated: true, completion: nil) 
    } 

} 

@IBAction func ButtonScegli(_ sender: UIButton) { 

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary; 
     imagePicker.allowsEditing = true 
     self.present(imagePicker, animated: true, completion: nil) 
    } 

} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [AnyHashable: Any]!) { 
    ImageView.image = image 
    self.dismiss(animated: true, completion: nil); 
} 

這是按鈕的功能「Vendi」:

if let user = FIRAuth.auth()?.currentUser{ 

     self.emailUser.text = user.email 
     let userID: String = user.uid 
     let x = libriArray.count 
     let y = String(x+1) 
     //let imageLibro: UIImage = self.ImageView.image! 
     let emailVenditore: String = self.emailUser.text! 
     let titoloLibro: String = self.TitoloText.text! 
     let codiceLibro: String = self.ISBNText.text! 
     let prezzoLibro: String = self.PrezzoText.text! 
     let edizioneLibro: String = self.EdizioneText.text! 
     let statoLibro: Bool = false 

     let Libro = ["titolo": titoloLibro, "codice": codiceLibro, "prezzo": prezzoLibro, "autore": edizioneLibro, "emailUser": emailVenditore, "userID": userID, "stato": statoLibro] as [String : Any] 

     let libriRef = ref.child(byAppendingPath: "Libri") 

     var libri = [y: Libro] 
     libriRef.childByAutoId().setValue(Libro) 

     self.dismiss(animated: true, completion: nil) 

    } else { 

    } 

有人能寫,解釋如何做到上傳和檢索這張照片?

+0

我假設你要上傳的照片火力地堡寄存,在這種情況下,過程記錄在這裏:HTTPS:/ /firebase.google.com/docs/storage/ios/upload-files。您還可以使用[適用於iOS的Firebase codelab](https://codelabs.developers.google.com/codelabs/firebase-ios-swift/#8),其中包含一個關於上傳圖片的步驟。 –

回答

0

我很難跟蹤你正在做什麼與這些var名稱。不過,我假設你從imagePickerController獲得了UIImage

您需要在相應文件中使用Firebase存儲(pod 'Firebase/Storage')和import FirebaseStorage

這裏是你可以做的UIImage上傳到火力地堡儲存什麼:

func uploadPhoto(_ image: UIImage, completionBlock: @escaping() -> Void) { 
    let ref = FIRStorage.storage().reference().child("myCustomPath").child("myFileName.jpg") // you may want to use UUID().uuidString + ".jpg" instead of "myFileName.jpg" if you want to upload multiple files with unique names 

    let meta = FIRStorageMetadata() 
    meta.contentType = "image/jpg" 

    // 0.8 here is the compression quality percentage 
    ref.put(UIImageJPEGRepresentation(image, 0.8)!, metadata: meta, completion: { (imageMeta, error) in 
     if error != nil { 
      // handle the error 
      return 
     } 

     // most likely required data 
     let downloadURL = imageMeta?.downloadURL()?.absoluteString  // needed to later download the image 
     let imagePath = imageMeta?.path  // needed if you want to be able to delete the image later 

     // optional data 
     let timeStamp = imageMeta?.timeCreated 
     let size = imageMeta?.size 

     // ----- should save these data in your database at this point ----- 

     completionBlock() 
    }) 

} 

這是一個簡單的功能,可以上傳到UIImage火力地堡存儲。請注意,您應該跟蹤downloadURL和您上傳的每張圖片的路徑。任何上傳後,您都可以將它們保存在數據庫中。

要下載你上傳的圖片,你可以做這樣的事情:

func retrieveImage(_ URL: String, completionBlock: @escaping (UIImage) -> Void) { 
    let ref = FIRStorage.storage().reference(forURL: URL) 

    // max download size limit is 10Mb in this case 
    ref.data(withMaxSize: 10 * 1024 * 1024, completion: { retrievedData, error in 
     if error != nil { 
      // handle the error 
      return 
     } 

     let image = UIImage(data: retrievedData!)! 

     completionBlock(image) 

    }) 
}