0
votes

I would like to be able to take a photo in view of my app. However, when I press the button, the screen becomes black. Could You please help me with that problem? I have the imageView and one button to make a photo.

I have such code:

var imagePicker: UIImagePickerController!       
@IBOutlet var imageView: UIImageView!

@IBAction func takePhoto(_ sender: UIButton) {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.camera
    imagePicker.allowsEditing = false

    let vc = ViewController()
    var navigationController = UINavigationController(rootViewController: vc)

    self.present(navigationController, animated: true, completion: nil)
}

All necessary delegates in class are added. Probably there is the mistake in self.present(), but in other cases it doesn't work.

self.presentViewController(imagePicker, animated: true, completion: nil) doesn't work in new version.

And second question: Is it able to see the camera screen directly in the view? Not only after pressing the button?

1

1 Answers

1
votes

You should be presenting UIImagePickerController. Currently you are presenting a navigation controller with an empty view controller and that's way screen goes black.

Try this..

var imagePicker: UIImagePickerController!       
@IBOutlet var imageView: UIImageView!

@IBAction func takePhoto(_ sender: UIButton) {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.camera
    imagePicker.allowsEditing = false        
    self.present(imagePicker, animated: true, completion: nil)
}