问题描述
我有一个动作表,它有两个选项:一个是您可以从库中选择图像,另一个是您可以从相机拍摄照片.照片库正常运行,但我似乎无法让相机工作.
I have an action sheet that has two options: one where you can choose an image from your library and one where you can take a photo from the camera. The photo library functions properly, but I can't seem to get the camera to work.
这是我的代码:
let takePhoto = UIAlertAction(title: "Take photo", style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
// present camera taker
var cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .Camera
self.presentViewController(cameraPicker, animated: true, completion: nil)
})
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
var selectedAvatar = UIImagePickerControllerOriginalImage
self.dismissViewControllerAnimated(false, completion: nil)
}
我似乎找不到问题,有人可以帮我吗?当我尝试运行该程序并单击拍照"时,该程序崩溃了.
I can't seem to find the problem, can anybody help me out? The program crashes when I try to run it and click on Take Photo.
推荐答案
您很可能在模拟器中运行.模拟器没有摄像头,因此在按下按钮时不会使应用程序崩溃,您必须检查 cemera 是否可用.
You are most likely running in the simulator. The simulator dont have a camera so to not make the app crash when pressing the button you have to check if cemera is available.
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
...
}
else {
print("Sorry cant take picture")
}
这篇关于UIImagePickerController 相机无法快速工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!