skip to Main Content

The associated view contains a circular image view in which the user taps a button and selects an image then that image is set to that image views corresponding image. If I attempt to force-unwrap the optional in saveChanges it returns an error saying only nil was found during runtime. Been battling this for a couple days. Definitely at my wits end.

import UIKit

class EditProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet var editBio: UITextField!
    @IBOutlet var editFullName: UITextField!
    @IBOutlet var chosenImage: UIImageView!
    
    let imagePicker = UIImagePickerController()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        makeRounded(imageView: chosenImage)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self
        // Do any additional setup after loading the view.
    }
    
    @IBAction func imagePicked(_ sender: UIButton) {
        
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            imagePicker.sourceType = .photoLibrary
            self.present(imagePicker, animated: true, completion: nil)
            }
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
            if let selectedImage = info[.originalImage] as? UIImage {
                chosenImage.image = selectedImage
            }
            dismiss(animated: true, completion: nil)
        }
    }
    @IBAction func saveChanges(_ sender: UIButton) {
       
        if let newChosenImage = chosenImage.image {
            ProfileStore.shared.userProfile.profilePicture = newChosenImage
        } else {
            print("uugghhh")
        }
        ProfileStore.shared.userProfile.fullName = editFullName.text!
        ProfileStore.shared.userProfile.description = editBio.text!
    }

2

Answers


  1. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

    should be outside of @IBAction func imagePicked(_ sender: UIButton) ‘s scope

    use the following code

    @IBAction func imagePicked(_ sender: UIButton) {
            
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicker.sourceType = .photoLibrary
                self.present(imagePicker, animated: true, completion: nil)
                }
            
            
        }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
                if let selectedImage = info[.originalImage] as? UIImage {
                    chosenImage.image = selectedImage
                }
                dismiss(animated: true, completion: nil)
            }
    

    instead of

    @IBAction func imagePicked(_ sender: UIButton) {
            
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicker.sourceType = .photoLibrary
                self.present(imagePicker, animated: true, completion: nil)
                }
            // it should not be within ` @IBAction func imagePicked(`
            // it should be outside  ` @IBAction func imagePicked(`
            func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
                if let selectedImage = info[.originalImage] as? UIImage {
                    chosenImage.image = selectedImage
                }
                dismiss(animated: true, completion: nil)
            }
        }
    

    then OK

    Login or Signup to reply.
  2. I can see that delegate method of picker controller is written inside button it should be outside of button scope, please compare this to your posted code

    class EditProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
        @IBOutlet var editBio: UITextField!
        @IBOutlet var editFullName: UITextField!
        @IBOutlet var chosenImage: UIImageView!
        
        let imagePicker = UIImagePickerController()
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            makeRounded(imageView: chosenImage)
        }
        
        override func viewDidLoad() {
            super.viewDidLoad()
            imagePicker.delegate = self
            // Do any additional setup after loading the view.
        }
        
        @IBAction func imagePicked(_ sender: UIButton) {
            
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicker.sourceType = .photoLibrary
                self.present(imagePicker, animated: true, completion: nil)
                }}
            
            func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
                if let selectedImage = info[.originalImage] as? UIImage {
                    chosenImage.image = selectedImage
                }
                dismiss(animated: true, completion: nil)
            }
        
        @IBAction func saveChanges(_ sender: UIButton) {
           
            if let newChosenImage = chosenImage.image {
                ProfileStore.shared.userProfile.profilePicture = newChosenImage
            } else {
                print("uugghhh")
            }
            ProfileStore.shared.userProfile.fullName = editFullName.text!
            ProfileStore.shared.userProfile.description = editBio.text!
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search