This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var unicorn: Unicorn? { | |
didSet { | |
if let unicorn = unicorn { | |
downloadImage(from: unicorn.imagePath) | |
addedBy.text = unicorn.addedBy | |
seenAt.text = unicorn.seenAt | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import FirebaseDatabase | |
struct Unicorn { | |
let imagePath: String | |
let addedBy: String | |
let seenAt: String | |
// Standard init | |
init(imagePath: String, addedBy: String, seenAt: String) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ref: DatabaseReference! | |
ref = Database.database().reference() |
NewerOlder