简体   繁体   中英

Swift - black screen in ios application, which takes photo

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?

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM