【问题标题】:Uiimagepicker For showing camerauiimagepicker 用于显示相机
【发布时间】:2010-11-03 10:09:57
【问题描述】:

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

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

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

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

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

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

【问题讨论】:

    标签: iphone-sdk-3.0


    【解决方案1】:

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

    以下可以检查设备是否有相机可用。

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
    }
    

    您无法从模拟器中检查相机功能。您可以指定UIImagePickerControllerSourceTypePhotoLibrary 作为 sourceType 在模拟器上进行测试。

    【讨论】:

    • 谢谢 Chaitanya 我已经做到了,但我想知道该应用程序如何在模拟器上显示相机视图,但由于需要硬件而无法点击。
    • 操作系统需要驱动程序来操作外部设备。 iOS 没有 MacBook 摄像头的驱动程序。
    【解决方案2】:

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

      在发生异常的地方放置代码。记住你需要实现 navigationController

       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];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-16
        • 2011-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多