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
}
}
The Firebase SDKs for Cloud Storage add Google security to file uploads and downloads for your Firebase apps, regardless of network quality. You can use our SDKs to store images, audio, video, or other user-generated content. On the server, you can use Google Cloud Storage APIs to access the same files.
After that click on the Upload file option to insert an image on firebase storage. After that click on the image inserted then the image details come in the right section then click on the access token and copy the image URL.
Steps to Retrieve Image from Firebase in Realtime. Step 1: Create a New Project ... Step 2: Connect your app to firebase . In the android studio, go to the Tools option in the topmost bar then click on the firebase option then click the connect to firebase button. Refer to know how to connect the app to firebase.
After a successful upload, we can fetch the download URL of the uploaded image and then fetch it with any image loader, e.g. Kingfisher. There could be errors during uploading files to Cloud Storage for Firebase at both server and client sides. We can handle the errors with the observe function.
I’ll be using StatefulWidget to load image since I'll be toggling between two images available in my FirebaseStorage bucket. FutureBuilder widget is used to fetch the image from the Storage, and display the image depending on the connectionState and successful image retrieval.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With