在iPhone应用程序中检测相机的存在?

时间:2010-09-04 20:46:14

标签: ios objective-c ios-camera

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

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

8 个答案:

答案 0 :(得分:158)

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

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
   // Has camera

答案 1 :(得分:21)

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

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

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

答案 2 :(得分:19)

是的,提供了一个API来做到这一点:

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

答案 3 :(得分:19)

SWIFT 3

正如 Juan Boero 所写,请检查:

    if UIImagePickerController.isSourceTypeAvailable(.camera){ }

但是我会添加另一张检查,看看用户是否允许访问相机,就像Apple在PhotoPicker示例中所建议的那样(PhotoPicker example Objective-C):

*请注意您必须导入AVFoundation

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)
}

答案 4 :(得分:11)

<强>夫特:

add

答案 5 :(得分:6)

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

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];

答案 6 :(得分:-1)

检查相机是否可用(Swift)

if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))

答案 7 :(得分:-1)

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

let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
let isWideAngleCameraSupported = !discovery.devices.isEmpty