Detect existence of camera in iPhone app?

前端 未结 8 737
逝去的感伤
逝去的感伤 2020-12-23 15:49

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

相关标签:
8条回答
  • 2020-12-23 16:24

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

    isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
    
    0 讨论(0)
  • 2020-12-23 16:27

    To check of camera is available (Swift)

    if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
    
    0 讨论(0)
  • 2020-12-23 16:32

    Swift:

    if UIImagePickerController.isSourceTypeAvailable(.Camera){
    
        //Your code goes here
        //For example you can print available media types:
    
        print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))
    
        }
    
    0 讨论(0)
  • 2020-12-23 16:33

    You can use +isSourceTypeAvailable: method in UIImagePickerController:

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
       // Has camera
    
    0 讨论(0)
  • 2020-12-23 16:34

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

    BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    
    0 讨论(0)
  • 2020-12-23 16:41

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

    let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
    let isWideAngleCameraSupported = !discovery.devices.isEmpty
    
    0 讨论(0)
提交回复
热议问题