im trying to use imagePickerController and im getting this weird error "Cannot infer contextual base in reference to member ‘originalImage’"
I’ve used this code before but im not sure why im getting this error this time, or how to fix it.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
guard let pickImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: (info)")
}
photoImageView.image = pickImage
dismiss(animated: true, completion: nil)
}
please help!
heres the full code:
class AddCardViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var saveButton: UIBarButtonItem!
@IBOutlet weak var questionField: UITextField!
@IBOutlet weak var answerField: UITextField!
@IBOutlet weak var photoImageView: UIImageView!
private let imagePickerController = UIImagePickerController()
let defaultImage = UIImage(named: "q")
private var newCard: Card?
private func updateSaveButtonState() {
let questionText = questionField.text ?? ""
let answerText = answerField.text ?? ""
saveButton.isEnabled = !questionText.isEmpty && !answerText.isEmpty
}
public func getCard() -> Card? {
return newCard
}
override func viewDidLoad() {
super.viewDidLoad()
photoImageView.image = defaultImage
questionField.delegate = self
answerField.delegate = self
}
@IBAction func pickImage(_ sender: Any) {
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let pickImage:UIImage = info [.originalImage] as? UIImage else {
fatalError("expected a dictionary containing an image, but was provided the following: (info)")
}
photoImageView.image = pickImage
dismiss(animated: true, completion: nil)
}
func alertMessage(_ message: String) {
let alertController = UIAlertController(title: "Add a card", message: message, preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "okie dokie", style: UIAlertAction.Style.default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
2
Answers
The issue was I was using Swift 4 when I should've been using Swift 5.
I just checked your code and it works for me.
It could be that you don’t have an IBOutlet for
photoImageView
If it isn’t that, then I would need to see the rest of your
imagePickerController
code to understand what’s wrong.