Ghiggz Pikkoro Ghiggz Pikkoro - 3 years ago 128
iOS Question

How to download a photo from Firebase Storage and replace the last one for the current user

I'm trying to download a photo user account from Firebase.
I explain my issue:
On the profil page of the user, there's a button showing his current image for his profil avatar, first there's no image in the button background, when he clicks the first time he could choose an image in his photo library or take a photo by the device camera (by alert controller), then this image or photo is uploaded in Firebase Storage, and the new photo is showing on the button background.

But when the user wants to change his current photo, he tap the button again and choose or take a photo, but after he did that, the button background doesn't change, it means user changes his photo but on the current page the photo doesn't update the changes, but if I go another view and come back on the change photo view, the right photo of the user just chooses are in the button background. So the download can works good but not immediately if users didn't go another view. It a little difficult to explain.

Here's my code for my upload way :

// Variables I need
@IBOutlet weak var photoProfil: UIButton!
var name: String!
var userUid: String!
@IBOutlet var userImagePicker: UIImageView!
var imagePicker: UIImagePickerController!
var imageSelected = false
var isUploaded: Bool = false
var refDatabase: DatabaseReference!
var photovide: UIImage = UIImage(named:"profilvide")!

// Take a photo with the camera (first choice)
func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera // Source Camera
self.present(imagePicker, animated: false, completion: nil)
} else { // If camera doesn't work
imagePicker.delegate = self
imagePicker.allowsEditing = false

imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary // Source Library
self.present(imagePicker, animated: false, completion: nil)
}
}

// Choose an image from the library
func libraryPhoto(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
}

// Take this image and shows it on the View Controller
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
// I want to upload the photo to storage and save it in the database
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
photoProfil.setImage(image, for: .normal) // Button showing the user photo
userImagePicker.image = image
imageSelected = true
uploadImg()
} else {
print("Error...")
}

imagePicker.dismiss(animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
}

// The alert Controller
@IBAction func actionButton(_ sender: Any) {
let attributedString = NSAttributedString(string: "User photo", attributes: [
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15),
NSForegroundColorAttributeName : UIColor.black
])

let alertController = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
alertController.message = nil
alertController.setValue(attributedString, forKey: "attributedTitle")
alertController.addAction(UIAlertAction(title: "Take photo", style: .default, handler: self.takePhoto))
alertController.addAction(UIAlertAction(title: "Choose in Library", style: .default, handler: self.libraryPhoto))
alertController.addAction(UIAlertAction(title: "Show current photo", style: .default, handler: self.showPhoto))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}

// Create a path in order to save the photo in Firebase
func setUser(img: String) {
var userUid = Auth.auth().currentUser?.uid
let userData = [
"nickname": Auth.auth().currentUser?.displayName,
"userImg": img
]

KeychainWrapper.standard.set(userUid!, forKey: "uid")
let location = Database.database().reference().child("users").child(userUid!).child("pseudo")
location.setValue(userData)
dismiss(animated: true, completion: nil)
}

// Upload and put image in the Firebase Storage
func uploadImg() {
guard let img = userImagePicker.image, imageSelected == true else {
print("Image needs to be selected")
return
}

if let imgData = UIImageJPEGRepresentation(img, 0.2) {
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { (metadata, error) in
if error != nil {
print("Did not upload img")
self.isUploaded = false
} else {
print("uploaded")
self.isUploaded = true
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL {
self.setUser(img: url)
}
}
}
}
}


Here's the code for the download way and show photo after downloaded :

override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true

// For download photo from Firebase
name = Auth.auth().currentUser?.displayName
userUid = Auth.auth().currentUser?.uid

refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.hasChild("userImg") {
self.isUploaded = true
print("True userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("isUploaded2 = false")
print("false userImg doesn't exist")
print("Aucune photo n'a été uploadée dans la base de données")
}
})
}

override func viewWillAppear(_ animated: Bool) {
self.userImagePicker.image = nil
guard let username = Auth.auth().currentUser?.displayName else { return }
self.navigationItem.title = username

refDatabase = Database.database().reference().child("users").child(userUid!)

refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.hasChild("userImg") {
self.isUploaded = true
print("isUploaded2 = true")
print("true userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("False userImg doesn't exist")
}
})
}

func downloadPhoto(user: String) {
self.name = user
let recipientData = Database.database().reference().child("users").child(userUid!).child("pseudo")

recipientData.observeSingleEvent(of: .value, with: { (snapshot) in
let data = snapshot.value as! Dictionary<String, AnyObject>
let nickname = data["nickname"]
let userImg = data["userImg"]

let ref = Storage.storage().reference(forURL: userImg! as! String)
ref.getData(maxSize: 1000000, completion: { (data, error) in
if error != nil {
print("Could not load image")
} else {
if let imgData = data {
if let img = UIImage(data: imgData) {
self.userImagePicker.image = img
self.photoProfil.setImage(img, for: .normal)
}
}
}
})
})
userImagePicker.reloadInputViews()
}

func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}

// Try to do the same with the viewDidLoad() method for download but can't work
override func viewDidAppear(_ animated: Bool) {
refDatabase = Database.database().reference().child("users").child(userUid!)

refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in

if snapshot.hasChild("userImg") {
self.isUploaded = true
print("True userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("False userImg doesn't exist")
}
})
}

Answer Source

You should try to call the downloadPhoto() method in the uploadImg() method.

Like this :

func uploadImg() {
    name = Auth.auth().currentUser?.displayName
    userUid = Auth.auth().currentUser?.uid

    guard let img = userImagePicker.image, imageSelected == true else {
        print("Image needs to be selected")
        return
    }

    if let imgData = UIImageJPEGRepresentation(img, 0.2) {
        let imgUid = NSUUID().uuidString
        let metadata = StorageMetadata()
        metadata.contentType = "image/jpeg"

        Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { (metadata, error) in
            if error != nil {
                print("Did not upload img")
                self.isUploaded = false
            } else {
                print("Uploaded")
                self.isUploaded = true
                let downloadURL = metadata?.downloadURL()?.absoluteString
                if let url = downloadURL {
                    self.setUser(img: url)
                    self.downloadPhoto(user: self.name) // Here add it
                }
            }
        }
    }
}

You could too make a function with a piece of code you use in viewDidLoad() and in viewWillAppear() and call this function in this 2 methods for proper code. Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download