当在ios模拟器上运行时,当imagePicker的源代码是Camera时,应用程序崩溃了?
当我将imagePicker的源类型设置为Camera时,我的应用在模拟器上崩溃,但在设备上正常工作。我已经在'plist'中添加了相机和照片库的许可。当在ios模拟器上运行时,当imagePicker的源代码是Camera时,应用程序崩溃了?
崩溃日志:
终止应用程序由于未捕获的异常 'NSInvalidArgumentException' 的,理由是: '源型不可用'
代码:
-(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];
}
自从发生这起事故以来我正在尝试访问模拟器上的相机?这里会有什么问题。我错过了什么?
在模拟器上
您可以使用UIImagePickerController
isSourceTypeAvailable
的方法来检查,如果给定的源类型是否可用,你不能访问摄像机。
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType
返回是如果设备支持指定的源类型;如果 指定的源类型不可用,则为NO。
由于媒体源可能不存在或可能不可用,因此设备可能并不总是支持所有源类型。例如,如果您 尝试从用户的库中选取图像,并且库为空,则此方法返回NO。同样,如果相机已经在 的使用中,此方法返回NO。
在尝试使用UIImagePickerController对象选取图像之前,必须调用此方法以确保所需的源 类型可用。
您可以添加检查源类型用或不使用下面的代码行:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
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];
}
else{
//it is simulator or device with no camera source
// handle accordingly
}
,所以你需要检查相机是否可用
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// do your stuff
} else {
//camera not available
}
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerController=[[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = YES;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}
else
{
//Camera not available
}
如果您在迅速工作,然后用这个码。
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];
}
希望这可以帮助,并有加入本的做法代码始终以避免崩溃在相机故障设备 ...
'UIAlertView'已过时,使用'UIAlertController'代替。 –
由于未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:'源类型1不可用' - 这是应用程序崩溃意味着模拟器中我们无法访问摄像头的原因。只能访问设备中的摄像头,下面的代码可以避免崩溃。
-(void)showImgPickerWithSourceType: (UIImagePickerControllerSourceType)sourceType
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = sourceType;
}
picker.allowsEditing = YES;
[(UINavigationController *)[[self.window.subviews objectAtIndex:0] nextResponder] presentViewController:picker animated:YES completion:nil];
,你能否告诉我们,系统崩溃日志吗? – iPeter
你能显示你的崩溃报告吗?所以我们可以知道你的问题。 – Nirmalsinh
***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,理由是:“源型不可用” – fAiSaL