How to know if iPhone camera is ready to take picture?

后端 未结 7 1910
清酒与你
清酒与你 2020-12-05 06:54

When [camera takePicture] is called before the camera is ready, it spits out this message:

UIImagePickerController: ignoring request to take pictu

7条回答
  •  一生所求
    2020-12-05 07:53

    As @djromero said there is a solution by using AVFoundation (but not instead of UIImagePickerController. You just use AVFoundation to get a notification back).

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(cameraIsReady:)
                                                 name:AVCaptureSessionDidStartRunningNotification object:nil];
    

    And then, once the camera is ready you got your notification:

    - (void)cameraIsReady:(NSNotification *)notification
    {   
        NSLog(@"Camera is ready...");
        // Whatever
    }
    

    I have just tested it by calling takePicture after UIImagePickerController presentation (where I got the 'camera is not ready' message) and right inside my notification callback, where it worked like a charm.

    Side-note:

    [camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] always returns Yes because it only checks that there is a camera device available. As a matter of fact, Apple recommends that you always must check that this returns Yes and that you have a non nil delegate (to provide a way to dismiss the picker through the standard interface) before you try to initialize and present the controller.

提交回复
热议问题