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 它,并且我听到相机拍照的声音)。覆盖视图显示得很好。但是,委托方法 didFinishPickingMediaWithInfo: 永远不会被调用,并且 imagePickerControllerDidCancel 也不会被调用。
那么,如何调用委托方法,或者通过重写 takePicture 方法来保存图片? (我不知道如何捕获这里的图片数据,谷歌似乎让我失败了)。我不禁觉得我无法理解 UIImagePickerController 的内部工作原理,但文档并没有太大帮助:
例如:
“您可以提供自定义叠加视图来显示自定义拍照界面,并且您可以除了图像选择器界面提供的默认控件之外,还可以显示您的自定义叠加视图,或者代替图像选择器界面提供的默认控件。”
或来自 showCameraControls:
“如果将此属性设置为 NO 并提供自己的自定义控件,则可以在关闭图像选择器界面之前拍摄多张照片。” - 如何关闭选择器界面?
注意:委托在 IB 中设置正确,所以这不是问题。
感谢您提供的任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现你只需要在调用
takePicture
之前等待“足够长的时间”,否则它就会默默地失败。对于如何确定始终有效的“足够长”的最小值,我没有一个好的答案,但如果您设置一个计时器并等待五到十秒,您应该没问题。如果它直接从takePicture
或通过委托返回某种“我还没准备好拍照,抱歉”错误,那就太好了,但据我所知它没有't。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 fromtakePicture
or through the delegate, but as far as I know it doesn't.作为我自己问题的更新:事实证明,我太早尝试使用 takePicture 了。当我将操作移动到覆盖层上的按钮并从该按钮发送 takePicture 时(一旦选择器以模态方式呈现),委托方法将按其应有的方式触发。我不知道我想要的是否可以实现 - 无需按下该按钮即可自动拍摄图像 - 但如果可以,则可能需要在我尝试使用它之后的某个时间发送 takePicture 来完成。
As an update to my own question: It turns out that I was trying to use takePicture too early. When I moved the action to a button on the overlay and sent takePicture from that button (once the picker was presented modally), the delegate methods fired as they should. I don't know if what I wanted is achievable - taking the image without having to press that button, automatically - but if it is, it will probably have to be done by sending takePicture sometime after I was trying to use it.
-(void)imageMethod:(id)sender{
imagePickerController = [[UIImagePickerController 分配]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
-(void)imageMethod:(id)sender{
imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;