Skip to content

Instantly share code, notes, and snippets.

View downloadImageFunc.swift
func downloadImage(from storageImagePath: String) {
// 1. Get a filePath to save the image at
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let filePath = "file:\(documentsDirectory)/myimage.jpg"
// 2. Get the url of that file path
guard let fileURL = URL(string: filePath) else { return }
// 3. Start download of image and write it to the file url
storageDownloadTask = storageRef.child(storageImagePath).write(toFile: fileURL, completion: { (url, error) in
View unicornDidSet.swift
var unicorn: Unicorn? {
didSet {
if let unicorn = unicorn {
downloadImage(from: unicorn.imagePath)
addedBy.text = unicorn.addedBy
seenAt.text = unicorn.seenAt
}
}
}
View getUnicorns.swift
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ref.child("unicorns").observe(.value) { snapshot in
var unicorns = [Unicorn]()
for unicornSnapshot in snapshot.children {
let unicorn = Unicorn(snapshot: unicornSnapshot as! DataSnapshot)
unicorns.append(unicorn)
}
self.unicorns = unicorns
}
View writeUnicornToDatabaseMethod.swift
fileprivate func writeUnicornToDatabase(_ unicorn: Unicorn) {
// Access the "unicorns" child reference and then access (create) a unique child reference within it and finally set its value
ref.child("unicorns").child(unicorn.seenAt + "\(Int(Date.timeIntervalSinceReferenceDate * 1000))").setValue(unicorn.toAnyObject())
}
View didTapSubmit.swift
@IBAction func didTapSubmit(_ sender: UIButton) {
// Get properties for the unicorn-to-be-created
let addedBy = self.addedBy.text ?? ""
let seenAt = self.seenAt.text ?? ""
let unicorn = Unicorn(imagePath: storageImagePath, addedBy: addedBy, seenAt: seenAt)
// Create the unicorn and record it
writeUnicornToDatabase(unicorn)
// Return to Unicorns Table VC
View uploadSuccessMethod.swift
fileprivate func uploadSuccess(_ storagePath: String, _ storageImage: UIImage) {
// Update the unicorn image view with the selected image
unicornImageView.image = storageImage
// Updated global variable for the storage path for the selected image
storageImagePath = storagePath
// Enable submit button and change its color
submitButton.isEnabled = true
submitButton.backgroundColor = .green
View imagePickerControllerImplementation.swift
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
// 1. Get image data from selected image
guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage,
let imageData = UIImageJPEGRepresentation(image, 0.5) else {
print("Could not get Image JPEG Representation")
return
}
View updated-unicorn.swift
import Foundation
import FirebaseDatabase
struct Unicorn {
let imagePath: String
let addedBy: String
let seenAt: String
// Standard init
init(imagePath: String, addedBy: String, seenAt: String) {
View unicorn-updated.swift
import Foundation
struct Unicorn {
let imagePath: String
let addedBy: String
let seenAt: String
// Standard initializer
init(imagePath: String, addedBy: String, seenAt: String) {
self.imagePath = imagePath
View databaseReference.swift
var ref: DatabaseReference!
ref = Database.database().reference()