ios 源类型 1 不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13564027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:19:53  来源:igfitidea点击:

Source type 1 not available

iosipaduipickerviewuipopovercontrolleruipickerviewcontroller

提问by Allen Walker

I have an app for iPhone and iPad, and when I try to load an UIPickerViewControllerin a UIPopoverControllerfor iPad I get the Exception "Source type 1 not available". getting the problem even though using the device.

我有一个适用于 iPhone 和 iPad 的应用程序,当我尝试加载适用于 iPad的应用程序时UIPickerViewController,出现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];
}

回答by preetam

This is because you are opening camera on simulator... since the code is something like [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]and obviously simulator don't have camera... Proceed giving an alert like this,

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

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

Nothing to worry, it will work on device correctly!

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

回答by Pulsein Corporation

Are you trying to run the app in an iPhone emulator?

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

If that's the case, the emulator doesn't support camera functionality, and only supports getting photos from the photo library. Sounds like maybe I should build in an automatic fallback because lots of people will be trying to test their apps on the emulator.

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

回答by zaid afzal

You can not use the camera with the simulator only with a real device. The simulator does not have a camera even if the Mac has one.

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

Use the photo library instead

改用照片库

imagePicker.sourceType = .photoLibrary

instead of

代替

imagePicker.sourceType = .camera