Skip to content

Instantly share code, notes, and snippets.

@3257
Last active April 7, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3257/aebc4b18957e3b95caf34e01125b7aba to your computer and use it in GitHub Desktop.
Save 3257/aebc4b18957e3b95caf34e01125b7aba to your computer and use it in GitHub Desktop.
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
}
// 2. Create a unique image path for image. In the case I am using the googleAppId of my account appended to the interval between 00:00:00 UTC on 1 January 2001 and the current date and time as an Integer and then I append .jpg. You can use whatever you prefer as long as it ends up unique.
let imagePath = Auth.auth().app!.options.googleAppID + "/\(Int(Date.timeIntervalSinceReferenceDate * 1000)).jpg"
// 3. Set up metadata with appropriate content type
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
// 4. Show activity indicator
showNetworkActivityIndicator = true
// 5. Start upload task
storageUploadTask = storageRef.child(imagePath).putData(imageData, metadata: metadata) { (_, error) in
// 6. Hide activity indicator because uploading is done with or without an error
self.showNetworkActivityIndicator = false
guard error == nil else {
print("Error uploading: \(error!)")
return
}
self.uploadSuccess(imagePath, image)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment