I have coded this much and was able to get the UIImagePickerController
setup that when user selects photo that it automatically uploads it to their uid assigned them from Firestore
database, however when I go to another viewController and then switch back to the profile viewController the image has reset and I can go through the process again of picking an image of which it will upload to fire storage but will not remain in the UIImageView
upon returning to the profile page.
Any help on how to get the image to stay or how to fetch and display it in the UIImageView every time a user goes to their user profile page? Thanks
here is the code on how I allow the user to pick and how it uploads to fire storage.
extension ProfileViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let imageSelected = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
{
image = imageSelected
profileimage.image = imageSelected
} else if let imageOriginal = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
image = imageOriginal
profileimage.image = imageOriginal
}
guard let imageSelected = image?.pngData() else {
return
}
if Auth.auth().currentUser != nil {
storage.child("images/"+Auth.auth().currentUser!.uid+"+.png").putData(imageSelected, metadata: nil, completion: { _, error in
})
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
picker.dismiss(animated: true, completion: nil)
}
}
I solved my own question so I figured I would post the solution. The UIImagePickerController
code as listed in the question was correct and I am able to use this with one change to make the image load a lot faster. In place of the imageSelected = image?.pngData()
I replaced that with this...
guard let imageSelected = image?.jpegData(compressionQuality: 0.5) else { return }
and I also created a function to download the image and placing it inside of the UIImageView
each time a user revisits the profile page and if they choose they can simply click on the image and upload a new profile pic and the download images function keeps it set. Here is the function I used to accomplish this...
private func downloadimages() {
let reference = Storage.storage().reference(withPath: "\("images/"+Auth.auth().currentUser!.uid+"+.jpg")")
reference.downloadURL { (URL, error) in
let data = NSData(contentsOf: URL!)
let image = UIImage(data: data! as Data)
self.profileimage.image = image
}
}
I simply called this function in the
override func viewDidLoad() {
super.viewDidLoad()
}
and presto, works perfect!