简体   繁体   English

如何知道iPhone相机是否准备拍照?

[英]How to know if iPhone camera is ready to take picture?

When [camera takePicture] is called before the camera is ready, it spits out this message: [camera takePicture]准备好之前调用[camera takePicture] ,会发出以下消息:

UIImagePickerController: ignoring request to take picture; camera is not yet ready.

How can I know when it is ready to take a photo? 我怎么知道什么时候准备拍照?

[camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] always returns true, even when it's apparently not ready. [camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]总是返回true,即使它显然没有准备好。

As @djromero said there is a solution by using AVFoundation (but not instead of UIImagePickerController . You just use AVFoundation to get a notification back). 正如@djromero所说,有一个解决方案是使用AVFoundation (但不是 代替 UIImagePickerController 。你只需使用AVFoundation来获取通知)。

[[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. 我刚刚通过在UIImagePickerController演示之后调用takePicture测试它(在那里我得到了' takePicture没有准备好'消息)并且在我的通知回调中,它就像一个魅力。

Side-note : 旁注

[camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] always returns Yes because it only checks that there is a camera device available. [camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]始终返回Yes因为它只检查是否有可用的摄像机设备。 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. 事实上,Apple建议您在尝试初始化和呈现控制器之前,必须始终检查是否返回Yes并且您具有非零委托(以提供通过标准接口dismiss选择器的方法)。

To be honest I haven't tried it and the documentation is somewhat ambiguous, but what about [UIImagePickerController isCameraDeviceAvailable:...] ? 老实说,我没有尝试过,文档有些含糊不清,但是[UIImagePickerController isCameraDeviceAvailable:...]呢?

EDIT: As I just learned, this is not the solution for your problem. 编辑:正如我刚刚了解到的,这不是解决问题的方法。 Sorry, I thought it might be worth a try... 对不起,我觉得值得一试......

What @Alladinian said was most helpful, this answer is supplemental to that. @Alladinian所说的最有帮助的,这个答案是对此的补充。 I recommend using his AVCaptureSessionDidStartRunningNotification technique as that tells you when the camera is ready initially (but this is called only once.) I am also concerned with if the device is ready for subsequent pictures as well. 我建议使用他的AVCaptureSessionDidStartRunningNotification技术,因为它告诉你最初相机何时就绪(但只调用一次。)我也关心设备是否准备好后续图片。 I haven't found the best solution, because I don't see any call backs for the device that don't refer to the camera's session. 我没有找到最佳解决方案,因为我没有看到任何不参考相机会话的设备的回叫。 But this callback function seems to be good enough: 但是这个回调函数似乎已经足够好了:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

Note the camera most likely will be ready before that callback is called, and you can cram in one or two photos, but it seems that the camera will definitely be ready by the time the callback is called. 请注意,在调用回叫之前,相机很可能已准备好,并且您可以填写一张或两张照片,但看起来在调用回调时相机肯定会准备就绪。 This should keep you from getting the following error between multiple camera shots. 这可以防止您在多个相机拍摄之间出现以下错误。

UIImagePickerController: ignoring request to take picture; image is already being captured or camera not yet ready.

As per the documentation for UIImagePickerController. 根据UIImagePickerController的文档。 The takePicture() method is ready again when takePicture()方法再次准备就绪

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

is called. 叫做。 If your interested in blocking pictures during this time period just disable the button interface (button.userInterfaceEnabled = false) until the call returns with media. 如果您对在此时间段内阻止图片感兴趣,只需禁用按钮界面(button.userInterfaceEnabled = false),直到调用返回媒体。 I solved this very problem using the imagePickerController. 我使用imagePickerController解决了这个问题。

Here is a Swift Version : 这是一个Swift版本:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.cameraIsReady), name: AVCaptureSessionDidStartRunningNotification, object: nil)


func cameraIsReady(notification :NSNotification ) {
     print("Camera is ready...")
}

If you are using UIImagePickerController, following is the code for video, similar code implies for image as well. 如果您正在使用UIImagePickerController,以下是视频的代码,类似的代码也暗示图像。

UIImagePickerController *picker;
BOOL cameraIsOn;

Then check for camera device available 然后检查可用的相机设备

if ([UIImagePickerController isCameraDeviceAvailable:[picker cameraDevice]]) {
            if (cameraIsOn) {
                NSLog(@"stop camera");
                [picker stopVideoCapture];
                cameraIsOn = FALSE;
            }
            else {
                NSLog(@"start camera");
                [picker startVideoCapture];
                self.videoTimer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];
                cameraIsOn = TRUE;
            }
        }

This makes sure to start capturing video as soon as camera is ready 这样可以确保在相机准备就绪后立即开始捕获视频

imgpicker = [[UIImagePickerController alloc] init];  
[self presentViewController:imgpicker animated:YES completion:^(void){       
while(![imgpicker startVideoCapture]);
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM