iPhone:以编程方式拍照 [英] iPhone: taking a picture programmatically

查看:112
本文介绍了iPhone:以编程方式拍照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将OS 3.1中的UIImagePickerController接口与cameraOverlayView和takePicture一起使用,但是我显然无法理解其工作原理,因此无法获得所需的行为.

我要做的是打开相机并自动拍照,而无需用户与选择器交互或编辑图像.因此,我将UIImagePickerController子类化(类似于 http://github.com/pmark/Helpful-iPhone-Utilities/tree/master/BTL%20Utilities/),然后关闭所有控件:

- (void)displayModalWithController:(UIViewController*)controller animated:(BOOL)animated {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.toolbarHidden = YES;
    // Setting the overlay view up programmatically.
ipView = [[ImagePickerView alloc] init];
self.cameraOverlayView = ipView;
[controller presentModalViewController:self animated:NO];
}

在overlayView中,我设法强制触发UIImagePickerController的takePicture方法(我知道这一点,因为我可以进行NSLog,并听到相机拍照的声音). overlayView显示就好了.但是,永远不会调用委托方法didFinishPickingMediaWithInfo:,并且也不会调用imagePickerControllerDidCancel.

那么,我该如何获取要调用的委托方法,或者通过重写takePicture方法来保存图片? (我不知道如何在这里捕获图片数据,而Google似乎使我失败了).我忍不住感到无法理解UIImagePickerController的工作原理,但是这些文档并不太有用:

例如:

您可以提供一个自定义的叠加视图以显示一个自定义的拍照界面,并且可以从代码中启动图片的拍摄.您可以显示自定义的叠加视图,作为提供的默认控件的替代或替代通过图像选择器界面."

或通过showCameraControls:

如果将此属性设置为NO并提供自己的自定义控件,则可以在关闭图像选择器界面之前拍摄多张照片." -如何关闭选择器界面?

注意:代理已在IB中正确设置,所以这不是问题.

感谢您提供的任何帮助!

解决方案

我发现您只需在调用takePicture之前等待足够长的时间",否则它会自动失败.对于如何确定始终可用的足够长"的最小值,我没有很好的答案,但是如果您设置计时器并等待五到十秒,您应该可以.如果它直接从takePicture或通过委托返回了某种我还没准备好照相,对不起"错误,那将是很好的,但据我所知还没有.

I'm trying to use the UIImagePickerController interface from OS 3.1, with the cameraOverlayView and takePicture, but I've clearly failed to understand how this works, and so I'm not getting the behaviour I want.

What I want to do is open the camera and take a picture automatically without having to having the user interact with the picker or edit the image. So I subclass UIImagePickerController (similar to the example in http://github.com/pmark/Helpful-iPhone-Utilities/tree/master/BTL%20Utilities/) and turn off all of the controls:

- (void)displayModalWithController:(UIViewController*)controller animated:(BOOL)animated {
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.navigationBarHidden = YES;
self.toolbarHidden = YES;
    // Setting the overlay view up programmatically.
ipView = [[ImagePickerView alloc] init];
self.cameraOverlayView = ipView;
[controller presentModalViewController:self animated:NO];
}

In the overlayView, I've managed to force the takePicture method of UIImagePickerController to fire (I know this, because I can NSLog it, and I hear the sound of the camera taking a picture). The overlayView shows up just fine. However, the delegate method didFinishPickingMediaWithInfo: never gets called, and imagePickerControllerDidCancel doesn't get called either.

So, how do I either get the delegate methods to get called, or save the picture by overriding the takePicture method? (I have no idea how to capture the picture data here, and Google seems to have failed me). I can't help feeling that I've failed to understand how the guts of UIImagePickerController works, but the docs aren't overly helpful:

e.g.:

"You can provide a custom overlay view to display a custom picture-taking interface and you can initiate the taking of pictures from your code. Your custom overlay view can be displayed in addition to, or instead of, the default controls provided by the image picker interface."

or from showCameraControls:

"If you set this property to NO and provide your own custom controls, you can take multiple pictures before dismissing the image picker interface." - How do I dismiss the picker interface?

Note: the delegate is set properly in IB, so that's not the problem.

Thanks for any help you can provide!

解决方案

I've found that you just have to wait "long enough" before calling takePicture, or it just silently fails. I don't have a good answer for how to determine the minimum value of "long enough" that will always work, but if you set a timer and wait five or ten seconds you should be okay. It would be nice if it returned some kind of an "I'm not ready to take a picture yet, sorry" error either directly from takePicture or through the delegate, but as far as I know it doesn't.

这篇关于iPhone:以编程方式拍照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆