源类型1不可用

All*_*ker 37 uipickerview uipickerviewcontroller ipad uipopovercontroller ios

我有一个适用于iPhone和iPad的应用程序,当我尝试加载UIPickerViewController一个UIPopoverController用于iPad,我得到了异常"源类型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];
}
Run Code Online (Sandbox Code Playgroud)

pre*_*tam 102

这是因为你在模拟器上打开相机...因为代码是类似的[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
}
Run Code Online (Sandbox Code Playgroud)

没什么好担心的,它可以在设备上正常工作!

斯威夫特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
}
Run Code Online (Sandbox Code Playgroud)

  • 我从生产中的用户那里收到了这个崩溃。显然不在模拟器中。 (2认同)

小智 7

您不能仅在真实设备上使用带有模拟器的相机。即使 Mac 有摄像头,模拟器也没有摄像头。

使用照片库代替

imagePicker.sourceType = .photoLibrary
Run Code Online (Sandbox Code Playgroud)

代替

imagePicker.sourceType = .camera
Run Code Online (Sandbox Code Playgroud)