[英]UIImagePickerController camera not working swift
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. 照片库功能正常,但我似乎无法让相机工作。
Here is my code: 这是我的代码:
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. 当我尝试运行它并点击拍照时程序崩溃。
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. 模拟器没有相机,所以在按下按钮时不要让应用程序崩溃,你必须检查cemera是否可用。
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
...
}
else {
print("Sorry cant take picture")
}
Follow the below code 请遵循以下代码
func takePhoto()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
self .presentViewController(picker!, animated: true, completion: nil)
}
else
{
let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"")
alertWarning.show()
}
}
func openGallary()
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(picker!, animated: true, completion: nil)
}
//PickerView Delegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
println("picker cancel.")
}
There is another reason why the camera can crash when used in a real device as Kirit Modi said in the following tread: 当Kirit Modi在接下来的步骤中说,当在真实设备中使用时相机可能会崩溃还有另一个原因:
iOS 10 - App crashes To access photo library or device camera via UIImagePickerController iOS 10 - 应用程序崩溃通过UIImagePickerController访问照片库或设备相机
In iOS 10. You have to set privacy Setting for Camera & Photo Library. 在iOS 10.您必须为相机和照片库设置隐私设置。 Camera & Photo Privacy Setting at info.Plist info.Plist上的相机和照片隐私设置
This solved my crashing issue! 这解决了我崩溃的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.