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
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
should be outside of
@IBAction func imagePicked(_ sender: UIButton)
‘s scopeuse the following code
instead of
then OK
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