9

我有一個UIImagePicker設置在我的應用程序,工作正常。我想在我的UIImage選擇器被選中時上傳個人資料圖片到Firebase。這是我選擇照片時的功能。從UIImage Picker上傳圖片到新的Firebase(Swift)

//image picker did finish code 
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
    profilePic.contentMode = .ScaleAspectFill 
    profilePic.image = chosenImage 
    profilePic.hidden = false 
    buttonStack.hidden = true 
    changeButtonView.hidden = false 
    self.statusLabel.text = "Here is Your Profile Picture" 

    dismissViewControllerAnimated(true, completion: nil) 


} 

新文檔聲明我們需要聲明NSURl以上傳文件。這裏是我試圖找到給定文件的NSURL,但它不起作用。這裏是文檔和一個鏈接:https://firebase.google.com/docs/storage/ios/upload-files#upload_from_data_in_memory

// File located on disk 
let localFile: NSURL = ... 
// Create a reference to the file you want to upload 
let riversRef = storageRef.child("images/rivers.jpg") 

// Upload the file to the path "images/rivers.jpg" 
let uploadTask = riversRef.putFile(localFile, metadata: nil) { metadata, error in 
    if (error != nil) { 
    // Uh-oh, an error occurred! 
    } else { 
    // Metadata contains file metadata such as size, content-type, and download URL. 
    let downloadURL = metadata!.downloadURL 
    } 
} 

這裏是我的檢索UIImagePicker的NSURL嘗試:

//image picker did finish code 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

     let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
     //getting the object's url 
     let imageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL 
     let imageName = imageUrl.lastPathComponent 
     let documentDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! as String; 
     let photoUrl = NSURL(fileURLWithPath: documentDir) 
     let localPath = photoUrl.URLByAppendingPathComponent(imageName!) 
     self.localFile = localPath 

     profilePic.contentMode = .ScaleAspectFill 
     profilePic.image = chosenImage 
     profilePic.hidden = false 
     buttonStack.hidden = true 
     changeButtonView.hidden = false 
     self.statusLabel.text = "Here is Your Profile Picture" 

     dismissViewControllerAnimated(true, completion: nil) 


    } 

我相信,我也遇到了困難,如果圖像是因爲它尚未保存在設備上,而不是從相冊中取出。我如何找到這張圖片/快照的NSURL?

回答

24

這裏是我的方法來上傳和下載從火力存儲用戶的個人資料照片:

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { 
    userPhoto.image = image 
    dismissViewControllerAnimated(true, completion: nil) 
    var data = NSData() 
    data = UIImageJPEGRepresentation(userPhoto.image!, 0.8)! 
    // set upload path 
    let filePath = "\(FIRAuth.auth()!.currentUser!.uid)/\("userPhoto")" 
    let metaData = FIRStorageMetadata() 
    metaData.contentType = "image/jpg" 
    self.storageRef.child(filePath).putData(data, metadata: metaData){(metaData,error) in 
     if let error = error { 
      print(error.localizedDescription) 
      return 
     }else{ 
     //store downloadURL 
     let downloadURL = metaData!.downloadURL()!.absoluteString 
     //store downloadURL at database 
    self.databaseRef.child("users").child(FIRAuth.auth()!.currentUser!.uid).updateChildValues(["userPhoto": downloadURL]) 
     } 

     } 
        } 

我還存儲了圖像的URL到火力數據庫,並檢查用戶是否擁有個人資料照片或u可能會崩潰:

//get photo back 

    databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
      // check if user has photo 
      if snapshot.hasChild("userPhoto"){ 
       // set image locatin 
       let filePath = "\(userID!)/\("userPhoto")" 
       // Assuming a < 10MB file, though you can change that 
       self.storageRef.child(filePath).dataWithMaxSize(10*1024*1024, completion: { (data, error) in 

        let userPhoto = UIImage(data: data!) 
        self.userPhoto.image = userPhoto 
       }) 
      } 
     }) 
+0

你也可以創建這樣的路徑:let path =「\(NSUUID()。uuidString)」 – Neen

+0

@Neen偉大的建議!謝謝。 –

+0

上傳位的效果很好。下載不適合我。我必須以不同的方式做。 –

相關問題