简体   繁体   English

Swift-iOS应用程序中的黑屏,需要拍照

[英]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. 我有imageView和一个按钮来制作照片。

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.present()中可能存在错误,但在其他情况下则不起作用。

self.presentViewController(imagePicker, animated: true, completion: nil) doesn't work in new version. self.presentViewController(imagePicker,animation:true,completion:nil)在新版本中不起作用。

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 . 您应该展示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)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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