源类型1不可用

时间:2012-11-26 11:35:30

标签: ios ipad uipickerview uipopovercontroller uipickerviewcontroller

我有一个适用于iPhone和iPad的应用,当我尝试在UIPickerViewController中为iPad加载UIPopoverController时,我得到了“源类型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 个答案:

答案 0 :(得分:94)

这是因为你在模拟器上打开相机...... 因为代码类似于[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.init(title: nil, message: "Device has no camera.", preferredStyle: .alert)

    let okAction = UIAlertAction.init(title: "Alright", style: .default, handler: {(alert: UIAlertAction!) in
    })

    alertController.addAction(okAction)
    self.present(alertController, animated: true, completion: nil)

}
else{
     //other action
}

答案 1 :(得分:0)

您是否要在iPhone模拟器中运行该应用?

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

答案 2 :(得分:0)

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

改为使用照片库

imagePicker.sourceType = .photoLibrary

代替

imagePicker.sourceType = .camera

答案 3 :(得分:0)

即使您的Mac拥有,模拟器也不会配备摄像头。因此,尝试使用

picker.sourceType = .photoLibrary

代替

picker.sourceType = .camera 

将显示图片集。但不必担心,该代码将在实际设备上运行良好。