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

标签 ios swift firebase firebase-realtime-database firebase-storage

所以我是第一次使用 Firebase。我读过您应该将视频存储在存储中,然后将该唯一的 URL 存储在他们的数据库中。我将如何采用这种方法?例如,如果用户请求播放特定视频,我如何从数据库中获取 URL,然后使用该 URL 从数据库中提取视频?

感谢您的帮助,请原谅我对 Firebase 缺乏经验。

最佳答案

来自Zero to App talk at 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 设置到文本字段中:

// 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 方法将文本框中的文本发送到数据库。

该最小示例的完整代码在 this gist 中.

关于ios - Firebase:如何将视频存储在存储器中,然后将视频 URL 存储在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37901213/

相关文章:

ios - Xamarin.Forms(iOS项目):ListView分隔符显示在所有屏幕中

ios - 具有 WebView 的 native iOS 应用程序不请求相机权限

ios - 在巨大的 UICollectionView 中加载图片

xcode - 在 swift 中按枚举的值对对象数组进行排序

ios - 更新 CALayer 的旋转

ios - 使用 SwiftUI 成功登录后导航

ios - 如何处理多次写入,以便在一次写入失败时不提交数据?

javascript - Firebase - 需要修复 javascript sdk 身份验证错误

firebase - Firestore 导入 - 没有错误,但没有更改

android - 如何将 firebase 测试实验室与 circleci 集成