且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase:如何将视频存储在存储中,然后将视频URL存储在数据库中?

更新时间:2022-04-15 22:45:59

零在Google I/O上进行的应用对话附带以下代码:

// pragma mark - UIImagePickerDelegate overrides
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

  // Get local file URLs
  guard let image: UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
  let imageData = UIImagePNGRepresentation(image)!
  guard let imageURL: NSURL = info[UIImagePickerControllerReferenceURL] as? NSURL else { return }

  // Get a reference to the location where we'll store our photos
  let photosRef = storage.reference().child("chat_photos")

  // Get a reference to store the file at chat_photos/<FILENAME>
  let photoRef = photosRef.child("\(NSUUID().UUIDString).png")

  // Upload file to Firebase Storage
  let metadata = FIRStorageMetadata()
  metadata.contentType = "image/png"
  photoRef.putData(imageData, metadata: metadata).observeStatus(.Success) { (snapshot) in
    // When the image has successfully uploaded, we get it's download URL
    let text = snapshot.metadata?.downloadURL()?.absoluteString
    // Set the download URL to the message box, so that the user can send it to the database
    self.messageTextField.text = text
  }

  // Clean up picker
  dismissViewControllerAnimated(true, completion: nil)
}

这将获取在图像选择器中选择的图像,将其上传到Firebase存储,然后将该图像的结果下载URL设置为文本字段:

This takes the image that was selected in the image picker, uploads it to Firebase Storage and then sets the resulting download URL for that image into a text field:

// Send a chat message
func sendMessage(sender: AnyObject) {
  // Create chat message
  let chatMessage = ChatMessage(name: self.username, message: messageTextField.text!, image: nil)
  messageTextField.text = ""

  // Create a reference to our chat message
  let chatRef = database.reference().child("chat")

  // Push the chat message to the database
  chatRef.childByAutoId().setValue(["name": chatMessage.name, "message": chatMessage.message])
}

sendMessage 方法然后将文本从文本框中发送到数据库.

The sendMessage method then sends the text from the text box to the database.

该最小示例的完整代码在此要点中.

Full code for that minimal example is in this gist.