我有一个适用于iPhone和iPad的应用程序,当我尝试在用于iPad的UIPopoverController中加载一个UIPickerViewController时,我得到了异常“源类型1不可用”。即使使用设备也会遇到问题。
@try {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = NO;
self.tempComp = component;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[self presentModalViewController:imagePicker animated:YES];
}else {
// We are using an iPad
popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePicker];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:component.bounds inView:component permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Camera Non Disponibile" message:@"La camera non è disponibile" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
@catch (NSException *exception) {
NSLog(@"Cattura eccezione %@", exception);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Eccezione" message:[NSString stringWithFormat:@"%@", exception] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}发布于 2013-08-05 14:35:07
这是因为你正在模拟器上打开摄像头...由于代码类似于[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera],显然模拟器没有camera...继续给出这样的警报,
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
else{
//other action
}Swift 3:
if !UIImagePickerController.isSourceTypeAvailable(.camera) {
let alertController = UIAlertController(title: nil, message: "Device has no camera.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Alright", style: .default, handler: { (alert: UIAlertAction!) in
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
} else {
// Other action
}没什么好担心的,它会在设备上正常工作!
发布于 2019-07-03 16:22:34
您不能将摄像机与模拟器一起使用,只能与真实设备一起使用。模拟器没有摄像头,即使Mac上有摄像头。
请使用图片库
imagePicker.sourceType = .photoLibrary而不是
imagePicker.sourceType = .camera发布于 2019-02-02 14:42:58
您是否正在尝试在iPhone模拟器中运行该应用程序?
如果是这样的话,模拟器不支持相机功能,只支持从照片库中获取照片。听起来似乎我应该构建一个自动后备,因为很多人都会尝试在模拟器上测试他们的应用程序。
https://stackoverflow.com/questions/13564027
复制相似问题