将多个图像存储到Firebase中并获取URL [英] Storing multiple images into firebase and getting urls

查看:70
本文介绍了将多个图像存储到Firebase中并获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100张图像要存储在Firebase存储中,但我还需要从其中提取网址.有自动的方法吗?

I have 100 images that I want to store in firebase storage, but I also need to extract the urls from them. Is there an automatic way of doing it?

如果没有更好的服务提供商,可以上传大量图像并自动提取网址?

If not is there a better service provider that allows uploading a lot of images and extracting the url all automatically??

推荐答案

我强烈建议同时使用Firebase存储和Firebase实时数据库来完成此任务.下面是一些显示这些片段如何相互作用的代码:

I highly recommend using Firebase Storage and the Firebase Realtime Database together to accomplish this. Some code to show how these pieces interact is below (Swift):

共享:

// Firebase services
var database: FIRDatabase!
var storage: FIRStorage!
...
// Initialize Database, Auth, Storage
database = FIRDatabase.database()
storage = FIRStorage.storage()

上传:

let fileData = NSData() // get data...
let storageRef = storage.reference().child("myFiles/myFile")
storageRef.putData(fileData).observeStatus(.Success) { (snapshot) in
  // When the image has successfully uploaded, we get it's download URL
  // This "extracts" the URL, which you can then save to the RT DB
  let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
  // Write the download URL to the Realtime Database
  let dbRef = database.reference().child("myFiles/myFile")
  dbRef.setValue(downloadURL)
}

下载:

let dbRef = database.reference().child("myFiles")
dbRef.observeEventType(.ChildAdded, withBlock: { (snapshot) in
  // Get download URL from snapshot
  let downloadURL = snapshot.value() as! String
  // Create a storage reference from the URL
  let storageRef = storage.referenceFromURL(downloadURL)
  // Download the data, assuming a max size of 1MB (you can change this as necessary)
  storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
    // Do something with downloaded data...
  })
})

有关更多信息,请参见从零到应用程序:使用Firebase开发,以及相关的源代码,以获取有关如何执行此操作的实际示例.

For more information, see Zero to App: Develop with Firebase, and it's associated source code, for a practical example of how to do this.

这篇关于将多个图像存储到Firebase中并获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆