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

Ori*_*guy 61 objective-c ios ios-camera

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

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

Vla*_*mir 160

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

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
   // Has camera
Run Code Online (Sandbox Code Playgroud)

  • 小心:如果相机被"限制",即使存在相机,也会返回false. (3认同)
  • 我刚刚在iOS 10.3.2上测试了这个.如果相机在您的设备上受到限制,它将返回false.限制我的意思是转到设置>常规>限制并关闭相机(这通常由系统管理员完成).我相信这是苹果想要的预期行为,因为即使存在相机,它在技术上也不可用. (2认同)

Boa*_*kel 22

SWIFT 3

正如Juan Boero写的那样检查:

    if UIImagePickerController.isSourceTypeAvailable(.camera){ }
Run Code Online (Sandbox Code Playgroud)

但我会添加另一张检查,以查看用户是否允许访问相机,如苹果在其PhotoPicker示例中所建议的那样(PhotoPicker示例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)
}
Run Code Online (Sandbox Code Playgroud)

  • 自2018年5月起,授权状态可以是{authorized,notDetermined,denied,restricted}之一-上面没有处理受限状态(即管理员完全禁止用户使用相机)。如果这对您的用例很重要,则您可能希望使用“ switch”代替“ if {}”,而不是“ if}”。 (2认同)

pre*_*ett 21

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

BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);
Run Code Online (Sandbox Code Playgroud)

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

  • 这不一定有效.如果限制相机已经关闭,则可以将麦克风作为设备输出,但没有相机可用.最好用[[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count] (19认同)

Ben*_*tto 20

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

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
Run Code Online (Sandbox Code Playgroud)


Jua*_*ero 11

迅速:

if UIImagePickerController.isSourceTypeAvailable(.Camera){

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

    print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))

    }
Run Code Online (Sandbox Code Playgroud)


Raw*_*ean 6

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

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
Run Code Online (Sandbox Code Playgroud)