I am trying to allow users to take photos when the button action is clicked. I've added an UIImageView into storyboard which is to my View Controller. I've set the name for this control 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
}
CodePudding user response:
Your function is not inside your class!! You should put the function inside the class, like this:
class HomeScreenViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]){
// any code
}
}