Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何知道iPhone摄像头是否准备好拍照?_Iphone_Uiimagepickercontroller - Fatal编程技术网

如何知道iPhone摄像头是否准备好拍照?

如何知道iPhone摄像头是否准备好拍照?,iphone,uiimagepickercontroller,Iphone,Uiimagepickercontroller,当在相机准备就绪之前调用[camera takePicture]时,会弹出以下消息: UIImagePickerController:忽略拍照请求;摄像机尚未准备就绪。 我怎么知道什么时候可以拍照 [camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]总是返回true,即使它显然还没有准备好。老实说,我还没有尝试过,文档也有点模棱两可,但是[UIImagePickerControllerIsCameradevi

当在相机准备就绪之前调用
[camera takePicture]
时,会弹出以下消息:

UIImagePickerController:忽略拍照请求;摄像机尚未准备就绪。

我怎么知道什么时候可以拍照


[camera isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
总是返回true,即使它显然还没有准备好。

老实说,我还没有尝试过,文档也有点模棱两可,但是
[UIImagePickerControllerIsCameradeviceAvable:…]呢


编辑:正如我刚学到的,这不是解决你问题的方法。抱歉,我认为这可能值得一试…

正如@djromero所说,有一种解决方案是使用
AVFoundation
(但是不是而不是
UIImagePickerController
。您只需使用
AVFoundation
来获取通知)

然后,一旦相机准备好,您就会收到通知:

- (void)cameraIsReady:(NSNotification *)notification
{   
    NSLog(@"Camera is ready...");
    // Whatever
}
我刚刚在
UIImagePickerController
presentation(在那里我得到了“camera is not ready”(相机还没有准备好)之后调用了
takePicture
,并在我的通知回调中进行了测试,它就像一个魔咒一样工作

旁注


[摄像头isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
始终返回
Yes
,因为它只检查是否有可用的摄像头设备。事实上,苹果公司建议您在尝试初始化和显示控制器之前,必须始终检查该命令是否返回
Yes
,以及是否有非零委托(通过标准界面提供
解除拾取程序的方法)。

如果您使用的是UIImagePickerController,下面是视频的代码,类似的代码也适用于图像

UIImagePickerController *picker;
BOOL cameraIsOn;
然后检查摄像机设备是否可用

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;
            }
        }

@Alladinian所说的是最有帮助的,这个答案是对它的补充。我建议使用他的AVCaptureSessionDidStartRunningNotification技术,因为它会告诉您相机最初准备好的时间(但只调用一次)。我还担心设备是否也准备好拍摄后续照片。我还没有找到最好的解决方案,因为我没有看到任何不涉及相机会话的设备的回调。但这个回调函数似乎已经足够好了:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
请注意,在调用回调之前,相机很可能已经准备就绪,您可以塞进一张或两张照片,但在调用回调时,相机似乎肯定已经准备就绪。这将防止您在多个摄影机快照之间出现以下错误

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

这样可以确保在相机准备好后立即开始拍摄视频

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

根据UIImagePickerController的文档。takePicture()方法在

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

被称为。如果您有兴趣在这段时间内阻止图片,只需禁用按钮界面(button.userInterfaceeEnabled=false),直到调用返回媒体。我使用imagePickerController解决了这个问题。

这里是一个Swift版本:

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


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

类似问题-所选答案不正确,但另一个答案涉及增加延迟。这可能是一个很好的起点。此外,请参阅-当相机未准备好时,似乎有一个返回代码。检查结果,等待,然后再试一次。我当然希望延迟不是唯一的办法,尽管看起来是这样。当然有更好的解决方案……你能针对iOS 4+设备吗?有一个解决方案,但它需要的是AVFoundation而不是UIImagePickerController。是的,我正在寻找最低的iOS 4.2.1。是的,不幸的是没有解决这个问题。谢谢Stefan,回答得真棒。这是非常有用的。我还没有机会插上电源,但我从投票结果来看,我认为它是可行的。谢谢阿拉丁@杰姆斯基德莫尔,不客气!事实上,这个非常有用,苹果应该更好地记录它,或者至少在
UIImagePickerController
中提供更高级别的api(例如通过委托方法)。这在iOS 4.2.1中不起作用。没有错误,只是回调方法从未执行过。有什么想法吗?@JamesSkidmore嗯,真奇怪。它肯定在iOS5中工作,并且文档说明该通知可用于iOS4+。如果我找到什么东西,我会看一看并告诉你。(您已经导入了
AVFoundation
,对吗?)谢谢阿拉迪尼安。是的,
AVFoundation
已导入,但仍不工作。它是NSNotification.Name.avcaptureSessiondStartRunning:)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.cameraIsReady), name: AVCaptureSessionDidStartRunningNotification, object: nil)


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