Question

J'écris une application iOS, et je dois être en mesure de détecter si l'appareil dispose d'un appareil photo. Auparavant, je voudrais vérifier si l'appareil est un iPhone ou non, puisque seul l'iPhone a un appareil photo - mais avec le lancement de l'iPod Touch 4, ce n'est plus une option viable. Les fonctions d'applications sans caméra, mais la présence d'une caméra ajoute des fonctionnalités.

Alors, quelqu'un peut me fournir un code qui indique s'il y a un appareil photo ou non?

Était-ce utile?

La solution

Vous pouvez utiliser la méthode de +isSourceTypeAvailable: dans UIImagePickerController:

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

Autres conseils

SWIFT 3

Juan Boero a écrit le vérifier:

    if UIImagePickerController.isSourceTypeAvailable(.camera){ }

Mais j'ajouter une autre vérification pour voir si l'utilisateur autorisé à accéder à la caméra comme Apple suggère dans leur exemple PhotoPicker ( exemple PhotoPicker Objective-C ):

* S'il vous plaît noter que vous devez importer 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)
}

Si vous utilisez les classes Foundation AV au lieu de UIImagePickerController que vous pouvez faire:

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

Si vous utilisez UIImagePickerController il est sans doute pas la peine, puisque vous auriez à ajouter AVFoundation.framework à votre projet.

Oui, il y a une API fournie pour faire exactement cela:

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

Swift:

if UIImagePickerController.isSourceTypeAvailable(.Camera){

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

    print(UIImagePickerController.availableMediaTypesForSourceType(.Camera))

    }

Si vous avez besoin de savoir si l'appareil est spécifiquement un avant ou caméra arrière, utilisez ceci:

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];

Pour vérifier la caméra est disponible (Swift)

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

Vous pouvez vérifier la disponibilité d'un type de source spécifique en utilisant la session de découverte (Swift 5):

let discovery = AVCaptureDevice.DiscoverySession.init(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back)
let isWideAngleCameraSupported = !discovery.devices.isEmpty
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top