2016-12-24 1 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 코드 테이블] (https://codelabs.developers.google.com/codelabs/firebase-ios-swift/#8)을 사용할 수도 있습니다. –

답변

0

나는 당신이 그 var 이름들로 정확히 무엇을하는지 추적하는 데 어려움을 겪고있다. 그러나 나는 당신이 imagePickerController에서 UIImage을 가지고 있다고 가정하고 있습니다.

해당 파일에는 Firebase Storage (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) 

    }) 
}