I am trying to allow users to take photos when the button action is clicked. Ive added an UIImage into storyboard which has a UIImage linked to my View Controller.I’ve set the name for this image as imageView, yet when I implement imageView.image = image I am getting an error "Cannot find ‘imageView’ in scope" i’ve also tried to set this as self.imageView = image and I get the error "Cannot find self in scope". I am unsure why my imageView which is connected to the @IBOutlet is not being found?
import UIKit
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
var button: UIButton! {
imageView.backgroundColor = .secondarySystemBackground
self.button.backgroundColor = .systemBlue
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func didTapButton(){
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.delegate = self
present(picker, animated: true)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
picker.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
return
}
imageView.image = image
}
>Solution :
You function is not inside your class!! You should put inside it:
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]){
// any code
}
}