Home Issue setting default profile pic in Swift (iOS)
Reply: 1

Issue setting default profile pic in Swift (iOS)

Zodey Joseph
1#
Zodey Joseph Published in 2018-01-13 02:52:23Z

I am new to Swift/iOS development, so this might be a stupid question, but I can't seem to find how to do this correctly.

I am following a youtube guide on how to programmatically code (no storyboard) a login screen in Swift and register a user into a Firebase database. The basic outline of the design are as follows

Once the user clicks the icon of the cat with the crown, the image picker comes up and they can select a profile picture and then register like normal. If the user does not select an image from the picker, the cat with the crown icon gets loaded as the profile picture by default.

What I have been trying to do is make it so a different picture (not the cat, and not shown to the user on the login page) named "nedstark" is set as the default profile picture that is stored in the database.

The program uses LoginController.swift and LoginController + handlers.swift files to make this happen. I tried to add a conditional to the picker where if nothing is selected, the default profile pic is set to a specific default user pic (different than the cat), but it doesn't work.

This is the code that stores the photo and other User info into the Database

            let imageName = NSUUID().uuidString
        let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).jpg")

        if let profileImage = self.profileImageView.image, let uploadData = UIImageJPEGRepresentation(profileImage, 0.1) {

            storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

                if let error = error {
                    print(error)
                    return
                }

                if let profileImageUrl = metadata?.downloadURL()?.absoluteString {

                    let values = ["name": name, "email": email, "profileImageUrl": profileImageUrl]

                    self.registerUserIntoDatabaseWithUID(uid, values: values as [String : AnyObject])
                }
            })
        }

This is the code where I am trying to set the default profile pic

func handleSelectProfileImageView() {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.allowsEditing = true

    present(picker, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    var selectedImageFromPicker: UIImage?

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {
        selectedImageFromPicker = editedImage
    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {
        selectedImageFromPicker = originalImage
    }
    else {  //Testing if default profile pic set
        selectedImageFromPicker = UIImage(named: "nedstark")
    }

    if let selectedImage = selectedImageFromPicker {
        profileImageView.image = selectedImage
    }
    else {  //Testing if default profile pic set
        profileImageView.image = UIImage(named: "nedstark")
    }
    dismiss(animated: true, completion: nil)

}

I I know that this is a total noob question, but any help, tips, or pointers is greatly appreciated. Thanks!

Hitesh Agarwal
2#
Hitesh Agarwal Reply to 2018-01-13 03:33:55Z

You need to handle delegate method of cancel event. UIImagepickercontrolledelegate has another method imagePickerControllerDidCancel()

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    imageView.image = UIImage(named: "nedstark")
    dismiss(animated: true, completion: nil)
}

When you click on cancel button in pickerviewcontroller then didfinishPickingMedia function won't call. That's way your default image is not set when you tap on cancel button.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316606 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO