49

我有一个适用于 iPhone 和 iPad 的应用程序,当我尝试为 iPad 加载一个应用程序时,UIPickerViewControllerUIPopoverController得到了异常“源类型 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];
}
4

4 回答 4

120

这是因为您正在模拟器上打开相机...因为代码类似于[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
}

斯威夫特 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
}

不用担心,它会在设备上正常工作!

于 2013-08-05T06:35:07.253 回答
4

您不能仅通过真实设备将相机与模拟器一起使用。即使 Mac 有相机,模拟器也没有相机。

改用照片库

imagePicker.sourceType = .photoLibrary

代替

imagePicker.sourceType = .camera
于 2019-07-03T08:22:34.467 回答
1

您是否尝试在 iPhone 模拟器中运行该应用程序?

如果是这种情况,模拟器不支持相机功能,只支持从照片库中获取照片。听起来也许我应该建立一个自动回退,因为很多人会尝试在模拟器上测试他们的应用程序。

于 2019-02-02T06:42:57.630 回答
1

即使你的 Mac 有,模拟器也不会有摄像头。所以尝试使用

picker.sourceType = .photoLibrary

代替

picker.sourceType = .camera 

这将显示图片集。但不用担心,代码将在真实设备上运行良好。

于 2020-12-16T06:51:43.453 回答