检测 iPhone 应用程序中是否存在相机?
我正在编写一个 iOS 应用程序,我需要能够检测设备是否有摄像头。以前,我会检查该设备是否是 iPhone,因为只有 iPhone 才有摄像头 - 但随着 iPod Touch 4 的推出,这不再是一个可行的选择。该应用程序无需相机即可运行,但相机的存在增加了功能。
那么,谁能给我提供返回是否有相机的代码?
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.
So, can anyone provide me with code that returns whether there is a camera or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以在 UIImagePickerController 中使用
+isSourceTypeAvailable:
方法:You can use
+isSourceTypeAvailable:
method in UIImagePickerController:正如 Juan Boero 所写,检查:
但我会添加另一个检查,以查看用户是否允许访问相机,正如苹果在其 PhotoPicker 示例中建议的那样(PhotoPicker 示例 Objective-C):
*请注意您必须导入 AVFoundation
SWIFT 5
SWIFT 3
As Juan Boero wrote check the:
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):
*please note you have to import AVFoundation
SWIFT 5
SWIFT 3
如果您使用 AV Foundation 类而不是 UIImagePickerController,您可以这样做:
如果您使用 UIImagePickerController 它可能不值得,因为您必须将 AVFoundation.framework 添加到您的项目中。
If you are using the AV Foundation classes instead of UIImagePickerController you can do:
If you are using UIImagePickerController it probably isn't worth it, since you'd have to add AVFoundation.framework to your project.
是的,有一个 API 可以做到这一点:
Yes, there is an API provided to do just that:
斯威夫特:
Swift:
如果您需要知道设备是否有前置摄像头或后置摄像头,请使用以下命令:
If you need to know whether the device specifically has a front or rear camera, use this:
您可以使用发现会话 (Swift 5) 检查特定源类型的可用性:
You can check for availability of a specific source type using discovery session (Swift 5):
检查相机是否可用(Swift)
To check of camera is available (Swift)