在ios模拟器上运行时,当imagePicker的源是Camera时,应用程序崩溃?

在ios模拟器上运行时,当imagePicker的源是Camera时,应用程序崩溃?,ios,objective-c,Ios,Objective C,我的应用程序在模拟器上崩溃,但在设备上运行良好,当我将imagePicker的源类型指定为Camera时。我已经在“plist”中添加了照相机和照片库的权限 崩溃日志: -(void)showImgPickerWithSourceType:(UIImagePickerControllerSourceType)sourceType { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.d

我的应用程序在模拟器上崩溃,但在设备上运行良好,当我将imagePicker的源类型指定为Camera时。我已经在“plist”中添加了照相机和照片库的权限

崩溃日志:

-(void)showImgPickerWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}

由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“源类型1不可用”

代码:

-(void)showImgPickerWithSourceType:(UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = sourceType;
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}

当我试图在模拟器上访问摄像头时,是否发生了碰撞?这里的问题是什么。我错过了什么?

在模拟器上,您无法访问摄像头,因此需要检查摄像头是否可用

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// do your stuff
} else {
//camera not available
}

您可以使用UIImagePickerController的
isSourceTypeAvailable
方法检查给定的源类型是否可用

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
如果设备支持指定的源类型,则返回YES;如果没有 指定的源类型不可用

因为媒体源可能不存在或不可用, 设备可能并不总是支持所有源类型。例如,如果你 尝试从用户的库中拾取图像,但该库已关闭 空,此方法返回NO。类似地,如果相机已处于 使用时,此方法返回NO

在尝试使用UIImagePickerController对象拾取图像之前,必须调用此方法以确保所需的源 类型可用。

if(UIImagePickerController.isSourceTypeAvailable(.camera))
{
         let myPickerController = UIImagePickerController()
         myPickerController.delegate = self
         myPickerController.allowsEditing = true
         myPickerController.sourceType = .camera
         self.present(myPickerController, animated: true, completion: nil)
}
else
{
         let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .alert)
         let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .cancel) { action -> Void  in
                    //Just dismiss the action sheet
         }
         actionController.addAction(cancelAction)
         self.present(actionController, animated: true, completion: nil)
}
您可以使用以下代码行添加检查源类型是否可用:

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = sourceType;
    picker.allowsEditing = YES;
    [(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];     

}
else{

    //it is simulator or device with no camera source

    // handle accordingly

}

参考资料:

如果您在swift中工作,请使用此代码。

if(UIImagePickerController.isSourceTypeAvailable(.camera))
{
         let myPickerController = UIImagePickerController()
         myPickerController.delegate = self
         myPickerController.allowsEditing = true
         myPickerController.sourceType = .camera
         self.present(myPickerController, animated: true, completion: nil)
}
else
{
         let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .alert)
         let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .cancel) { action -> Void  in
                    //Just dismiss the action sheet
         }
         actionController.addAction(cancelAction)
         self.present(actionController, animated: true, completion: nil)
}

你们在模拟器上打开摄像头,很明显模拟器并没有摄像头。。。继续发出这样的警报,

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                      message:@"Device has no camera"
                                                     delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles: nil];

[myAlertView show];

}
else{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = NO;
    picker.sourceType = sourceType;
    picker.allowsEditing = YES;
    [(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
}

希望这可能会有所帮助,并有一个始终添加此代码的实践,以避免因未捕获的异常“NSInvalidArgumentException”而终止应用程序时发生故障,原因:“源类型1不可用”-这是应用程序崩溃的原因,意味着在模拟器中我们无法访问摄像头。只能访问设备中的摄像头,因此使用以下代码避免崩溃

-(void)showImgPickerWithSourceType:    (UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = sourceType;
}
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];

你能给我们看一下撞车日志吗?你能给我们看一下你的撞车报告吗?因此,我们可以了解您的问题。***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“源类型1不可用”您无法在模拟器上访问摄像头,因此您必须添加一项检查,以防设备包含摄像头或不包含摄像头,以避免崩溃报告中说
源类型1
不可用<代码>源类型1代表
摄像机
。您无法从模拟器访问摄像头,因此当您尝试从模拟器访问摄像头时,您的应用程序会崩溃,因为模拟器没有摄像头。
UIAlertView
已被弃用,请改用
UIAlertController
。为什么要将
picker.allowsEditing
设置两次?