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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3643949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:39:54  来源:igfitidea点击:

Detect existence of camera in iPhone app?

iosobjective-cios-camera

提问by Origamiguy

I'm writing an iOS app, and I need to be able to detect if the device has a camera. Previously, I would check if the device is an iPhone or not, since only the iPhone has a camera - but with the launch of the iPod Touch 4 this is no longer a viable option. The app functions without a camera, but the presence of a camera adds functionality.

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

So, can anyone provide me with code that returns whether there is a camera or not?

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

回答by Vladimir

You can use +isSourceTypeAvailable:method in UIImagePickerController:

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

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

回答by Boaz Frenkel

SWIFT3

斯威夫特3

As Juan Boerowrote check the:

正如Juan Boero所写,请检查:

    if UIImagePickerController.isSourceTypeAvailable(.camera){ }

But I would add another check to see if the user allowed access to camera as apple suggests in their PhotoPicker example (PhotoPicker example Objective-C):

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

*please note you have to import AVFoundation

*请注意,您必须导入 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)
}

回答by prewett

If you are using the AV Foundation classes instead of UIImagePickerController you can do:

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

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

If you are using UIImagePickerController it probably isn't worth it, since you'd have to add AVFoundation.framework to your project.

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

回答by Ben Zotto

Yes, there is an API provided to do just that:

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

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

回答by Juan Boero

Swift:

迅速:

if UIImagePickerController.isSourceTypeAvailable(.Camera){

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

    print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))

    }

回答by RawMean

If you need to know whether the device specifically has a front or rear camera, use this:

如果您需要知道设备是专门配备前置​​摄像头还是后置摄像头,请使用以下命令:

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];

回答by Usman Nisar

To check of camera is available (Swift)

检查相机是否可用(Swift)

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

回答by Jovan Stankovic

You can check for availability of a specific source type using discovery session (Swift 5):

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

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