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)
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)
pre*_*ett 21
如果您使用的是AV Foundation类而不是UIImagePickerController,则可以执行以下操作:
BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);
Run Code Online (Sandbox Code Playgroud)
如果您正在使用UIImagePickerController它可能是不值得的,因为您必须将AVFoundation.framework添加到您的项目中.
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)
如果您需要知道设备是否专门配备前置或后置摄像头,请使用:
isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28598 次 |
| 最近记录: |