将图像从UIImage Picker上传到新的Firebase(Swift)

我在我的应用程序中设置了UIImagePicker工作正常。 当我的UIImage选择器被选中时,我想将个人资料图片上传到Firebase。 这是我选择图片时的function。

//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?

以下是从firebase存储上传和下载用户个人资料照片的方法:

  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存储到firebase数据库中并检查用户是否有个人资料照片或者您可能会崩溃:

  //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 }) } })