我正在寻找一个完整的代码示例,以从Firebase Storage检索图像,仅显示图像。可以作为图像视图或表格。我已经查看了这里的帖子和各种教程。总感觉缺少某些东西。如果我能看到整个过程,我就能更好地理解它。
附加的代码是我当前尝试将photo1从本地更改为从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
}
}