Тип источника 1 не доступен
У меня есть приложение для 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];
}
Ответы
Ответ 1
Это потому, что вы открываете камеру на симуляторе...
так как код что-то вроде [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.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
}
Ничего страшного, на устройстве будет работать правильно!
Ответ 2
Вы пытаетесь запустить приложение в эмуляторе iPhone?
В этом случае эмулятор не поддерживает функции камеры, а поддерживает только получение фотографий из библиотеки фотографий. Похоже, мне следует встроить автоматический запасной вариант, потому что многие люди будут пытаться протестировать свои приложения на эмуляторе.
Ответ 3
Вы не можете использовать камеру с симулятором только с реальным устройством. У симулятора нет камеры, даже если у Mac она есть.
Вместо этого используйте библиотеку фотографий
imagePicker.sourceType = .photoLibrary
вместо
imagePicker.sourceType = .camera