Swift - ios应用程序中的黑屏,拍照

我希望能够根据我的应用拍摄照片 . 但是,当我按下按钮时,屏幕变黑 . 你可以帮我解决这个问题吗?我有imageView和一个按钮来制作照片 .

我有这样的代码:

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

课堂上所有必要的代表都会被添加 . 可能self.present()中存在错误,但在其他情况下它不起作用 .

self.presentViewController(imagePicker,animated:true,completion:nil)在新版本中不起作用 .

第二个问题:是否能够直接在视图中看到相机屏幕?不仅在按下按钮后?

回答(1)

2 years ago

你应该出席 UIImagePickerController . 目前,您正在展示一个带有空视图控制器的导航控制器,并且屏幕变黑 .

试试这个..

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