使用Swift从Firebase存储中检索图像 [英] Retrieving image from Firebase Storage using Swift

查看:139
本文介绍了使用Swift从Firebase存储中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个从Firebase存储中检索图像的开始到结束代码示例,只是为了显示图像。作为图像视图或表格。我看过这里的帖子和各种教程。它总是觉得有些东西是遗漏的。如果我能看到整个图片,我将能够更好地把握这一点。



附加的代码是我目前尝试将photo1从本地更改为从Firebase存储中提取的。

 导入UIKit 
导入Firebase
导入FirebaseAuth
导入FirebaseStorage
导入FirebaseDatabase
$ b $ class MainMenuTableViewController:UITableViewController {



var mainMenu = [Menu]()
var photo1 = UIImage()
重写func viewDidLoad(){
super.viewDidLoad()
loadMenu()
}

func loadMenu(){

let storage = FIRStorage.storage()
//从URL创建一个存储引用
let storageRef = storage.referenceForURL(https://firebasestorage.googleapis.com/v0/b/medicalpatientapp-7fd45。 appspot.com/o/iconimages%2Ffile-medical-icons.png?alt=media&token=c95b9c51-67ae-4e93-b63c-62091015a9ff)
//下载数据,假设最大大小为1MB(您可以根据需要进行更改)
storageRef.dataWithMaxSize(1 * 1024 * 1024){(data,error) - > void
//创建一个UIImage,将它添加到数组
let pic = UIImage(data:data!)
self.photo1 = pic!

}


//让photo1 = UIImage(命名为:iconimages-file-medical-icons)!
let menu1 =菜单(名称:我的笔记,照片:photo1)!

让photo2 = UIImage(命名为:iconimages-file-medical-icons)!
让menu2 =菜单(名称:查看患者,照片:photo2)!

让photo3 = UIImage(名为:iconimages-add-medical-icons)!
let menu3 =菜单(名称:添加人员,照片:photo3)!
$ b $ mainMenu + = [menu1,menu2,menu3]


$ b重写func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning
//处理任何可以重新创建的资源。
}

// MARK: - 表视图数据源

覆盖函数numberOfSectionsInTableView(tableView:UITableView) - > Int {
// #warning不完整的实现,返回部分的数目
返回1
}

重写func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
// #warning不完整的实现,返回行数
return mainMenu.count
}


重写func tableView(tableView:UITableView ,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {


//配置单元格...
let cellIdentifier =MenuTableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath:indexPath)as ! MainMenuTableViewCell

//获取数据源布局的相应用餐。
let menu = mainMenu [indexPath.row]

cell.menuLabel.text = menu.name
cell.menuImage.image = menu.photo

返回单元



$ p

解决方案

我们强烈建议使用Firebase存储和Firebase实时数据库来完成此操作。以下是一个完整的示例:

共享:

  // Firebase服务
var database:FIRDatabase!
var storage:FIRStorage!
...
//初始化数据库,Auth,存储
database = FIRDatabase.database()
storage = FIRStorage.storage()
...
//为图片初始化一个数组
var picArray:[UIImage]()

上传:

  let fileData = NSData()//获取数据... 
let storageRef = storage.reference ().child(myFiles / myFile)
storageRef.putData(fileData).observeStatus(.Success){(snapshot)in
//当图片成功上传时,我们得到它的下载网址
let downloadURL = snapshot.metadata?.downloadURL()?absoluteString
//将下载URL写入实时数据库
let dbRef = database.reference()。child(myFiles / myFile)
dbRef.setValue(downloadURL)
}

下载

  let dbRef = database.reference()。child(myFiles)
dbRef.observeEventType(.ChildAdded,withBlock :{(快照)在
//从s获取下载URL napshot
让downloadURL = snapshot.value()as! String
//从URL创建存储引用
let storageRef = storage.referenceFromURL(downloadURL)
//下载数据,假设最大大小为1MB(可以根据需要更改)
storageRef.dataWithMaxSize(1 * 1024 * 1024){(data,error) - > void
//创建一个UIImage,将它添加到数组
let pic = UIImage(data:data)
picArray.append(pic)
})
})

有关更多信息,请参阅从零开始到应用:使用Firebase进行开发,并且它是相关的源代码,这是一个实际的例子。


I am looking for a beginning to end code example of retrieving an image from Firebase Storage, just to show an image. Either as an image view or for a table. I have looked at posts on here and various tutorials. It always feels like something is left out. If I could see the whole picture, I will be able to grasp this better.

The attached code is my current attempt to change photo1 from local to pull from Firebase Storage.

import UIKit
import Firebase
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase

class MainMenuTableViewController: UITableViewController {



var mainMenu = [Menu]()
var photo1 = UIImage()
override func viewDidLoad() {
    super.viewDidLoad()
    loadMenu()
}

func loadMenu() {

    let storage = FIRStorage.storage()
    // Create a storage reference from the URL
    let storageRef = storage.referenceForURL("https://firebasestorage.googleapis.com/v0/b/medicalpatientapp-7fd45.appspot.com/o/iconimages%2Ffile-medical-icons.png?alt=media&token=c95b9c51-67ae-4e93-b63c-62091015a9ff")
    // Download the data, assuming a max size of 1MB (you can change this as necessary)
    storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
        // Create a UIImage, add it to the array
        let pic = UIImage(data: data!)
        self.photo1 = pic!

    }


   //let photo1 = UIImage(named: "iconimages-file-medical-icons")!
    let menu1 = Menu(name: "My Notes", photo: photo1)!

    let photo2 = UIImage(named: "iconimages-file-medical-icons")!
    let menu2 = Menu(name: "View Patients", photo: photo2)!

    let photo3 = UIImage(named: "iconimages-add-medical-icons")!
    let menu3 = Menu(name: "Add Persons", photo: photo3)!

    mainMenu += [menu1, menu2, menu3]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return mainMenu.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    // Configure the cell...
    let cellIdentifier = "MenuTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MainMenuTableViewCell

    // Fetches the appropriate meal for the data source layout.
    let menu = mainMenu[indexPath.row]

    cell.menuLabel.text = menu.name
    cell.menuImage.image = menu.photo

    return cell
}

}

解决方案

We highly recommend using Firebase Storage and the Firebase Realtime Database together to accomplish this. Here's a full example:

Shared:

// Firebase services
var database: FIRDatabase!
var storage: FIRStorage!
...
// Initialize Database, Auth, Storage
database = FIRDatabase.database()
storage = FIRStorage.storage()
...
// Initialize an array for your pictures
var picArray: [UIImage]()

Upload:

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
  let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
  // Write the download URL to the Realtime Database
  let dbRef = database.reference().child("myFiles/myFile")
  dbRef.setValue(downloadURL)
}

Download:

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
    // Create a UIImage, add it to the array
    let pic = UIImage(data: data)
    picArray.append(pic)
  })
})

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.

这篇关于使用Swift从Firebase存储中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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