使用Swift从Firebase存储中检索图像

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

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

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
}

}

我们强烈建议您同时使用Firebase存储和Firebase实时数据库来完成此任务.这是一个完整的例子:

共享:

// 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]()

上传

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

下载:

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

有关更多信息,请参阅Zero to App: Develop with Firebase,它是associated source code,以获取有关如何执行此操作的实际示例.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...