代码之家  ›  专栏  ›  技术社区  ›  Sabby

用于显示相机的uiImagePicker

  •  10
  • Sabby  · 技术社区  · 15 年前

    大家好,我正在尝试制作一个照相机应用程序。我这样做是因为

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    

    其中picker是uiImagePicker控制器的对象。

    但当运行代码时,应用程序将终止显示错误。

    由于未捕获异常“nsInvalidArgumentException”,正在终止应用程序,原因:“源类型1不可用”

    我在模拟器上使用这个。我知道在模拟器中检查摄像机是不可能的,但我们可以对此进行测试。我想可能是因为相机不可用,所以它会终止。 但是我看到一个应用程序有相同的代码,但是它运行在模拟器上,只是显示了摄像头的视图。 帮我解决这个问题。此外,我如何将我的自定义视图放在该应用程序的相机上?

    3 回复  |  直到 9 年前
        1
  •  21
  •   Chaitanya    15 年前

    在设置sourcetype之前,需要检查设备是否有可用的摄像头。

    以下内容可以检查设备是否有摄像头可用。

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
    }
    

    您不能从模拟器检查相机功能。你可以分配 UIImagePickerControllerSourceTypePhotoLibrary 作为要在模拟器上测试的源类型。

        2
  •  1
  •   Antoine Floury    10 年前

    银行代码:2.2

    if UIImagePickerController.isSourceTypeAvailable(.Camera) {
      imagePicker.delegate = self
      imagePicker.sourceType = .Camera
      presentViewController(imagePicker, animated: true, completion: nil)
    } else {
      print("The device has no camera")
    }
    

    保存的相册

    if UIImagePickerController.isSourceTypeAvailable(.SavedPhotosAlbum) {
      imagePicker.delegate = self
      imagePicker.sourceType = .SavedPhotosAlbum
      imagePicker.allowsEditing = false
      self.presentViewController(imagePicker, animated: true, completion: nil)
    }
    
        3
  •  0
  •   Pushp    10 年前

    在发生异常的代码下面。记住,您需要实现导航控制器

     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"ERROR" message:@"No Camera Avalible" preferredStyle:UIAlertControllerStyleAlert];
    
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
                [self dismissViewControllerAnimated:alertView completion:nil];
            }];
            [alertView addAction:ok];
            [self.navigationController presentViewController:alertView animated:YES completion:nil];
        }