70

我正在编写一个 iOS 应用程序,我需要能够检测设备是否有摄像头。以前,我会检查设备是否是 iPhone,因为只有 iPhone 有摄像头——但随着 iPod Touch 4 的推出,这不再是一个可行的选择。该应用程序在没有相机的情况下运行,但相机的存在增加了功能。

那么,任何人都可以向我提供返回是否有相机的代码吗?

4

8 回答 8

168

您可以使用+isSourceTypeAvailable:UIImagePickerController 中的方法:

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
   // Has camera
于 2010-09-04T21:09:13.620 回答
27

正如Juan Boero所写,请检查:

    if UIImagePickerController.isSourceTypeAvailable(.camera) {...}

但我会添加另一个检查,看看用户是否允许访问相机,正如苹果在他们的 PhotoPicker 示例(PhotoPicker 示例 Objective-C)中所建议的那样:

*请注意您必须导入 AVFoundation

斯威夫特 5

    let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    switch authStatus {
        /*
         Status Restricted -
         The client is not authorized to access the hardware for the media type. The user cannot change the client's status, possibly due to active restrictions such as parental controls being in place.
         */
    case .denied, .restricted:
        // Denied access to camera
        // Explain that we need camera access and how to change it.
        let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertController.Style.alert)

        let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)

        dialog.addAction(okAction)
        self.present(dialog, animated:true, completion:nil)
    case .notDetermined:
        // The user has not yet been presented with the option to grant access to the camera hardware.
        // Ask for it.
        AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (grantd) in
        // If access was denied, we do not set the setup error message since access was just denied.
           if grantd {
           // Allowed access to camera, go ahead and present the UIImagePickerController.
            self.showImagePickerForSourceType(sourceType: UIImagePickerController.SourceType.camera)
            }
        })
    case .authorized:
        // Allowed access to camera, go ahead and present the UIImagePickerController.
        self.showImagePickerForSourceType(sourceType: UIImagePickerController.SourceType.camera)
    @unknown default:
        break; //handle other status
    }

斯威夫特 3

let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
    
if authStatus == AVAuthorizationStatus.denied {
    // Denied access to camera
    // Explain that we need camera access and how to change it.
    let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)
        
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
        
    dialog.addAction(okAction)
    self.present(dialog, animated:true, completion:nil)
        
} else if authStatus == AVAuthorizationStatus.notDetermined {     // The user has not yet been presented with the option to grant access to the camera hardware.
    // Ask for it.
    AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
    // If access was denied, we do not set the setup error message since access was just denied.
       if grantd {
       // Allowed access to camera, go ahead and present the UIImagePickerController.
            self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
        }
    })
} else {
        
    // Allowed access to camera, go ahead and present the UIImagePickerController.
    self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)

}

func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
    
    let myPickerController = UIImagePickerController()
    myPickerController.delegate = self;
    myPickerController.sourceType = sourceType  
    self.present(myPickerController, animated: true, completion: nil)
}
于 2017-05-03T09:12:35.737 回答
23

如果您使用的是 AV Foundation 类而不是 UIImagePickerController,您可以执行以下操作:

BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);

如果您使用的是 UIImagePickerController 它可能不值得,因为您必须将 AVFoundation.framework 添加到您的项目中。

于 2014-01-22T10:58:56.240 回答
20

是的,有一个 API 可以做到这一点:

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
于 2010-09-04T21:09:20.263 回答
12

迅速:

if UIImagePickerController.isSourceTypeAvailable(.Camera){

    //Your code goes here
    //For example you can print available media types:

    print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))

    }
于 2015-11-13T16:56:03.863 回答
6

如果您需要知道设备是否专门有前置或后置摄像头,请使用:

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
于 2014-07-04T06:51:48.667 回答
-1

可以检查相机(Swift)

if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
于 2016-08-30T04:13:35.643 回答
-1

您可以使用发现会话(Swift 5)检查特定源类型的可用性:

let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
let isWideAngleCameraSupported = !discovery.devices.isEmpty
于 2019-08-11T13:02:09.800 回答