xcode 我如何使用 UIImagePickerController 来显示相机而不是拍照?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2600748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:16:20  来源:igfitidea点击:

How do I use UIImagePickerController just to display the camera and not take a picture?

objective-ciphonexcodeiphone-sdk-3.0uiimagepickercontroller

提问by Thomas

I'd like to know how to open the camera inside of a pre-defined frame (not the entire screen). When the view loads, I have a box, and inside it, I want to display what the camera sees. I don't want to snap a picture, just basically use the camera as a viewfinder. I have searched this site and have not yet found what I'm looking for. Please help.

我想知道如何在预定义的框架内(不是整个屏幕)打开相机。当视图加载时,我有一个盒子,在它里面,我想显示相机看到的东西。我不想拍照,基本上只是将相机用作取景器。我已经搜索过这个网站,但还没有找到我要找的东西。请帮忙。

Thanks!
Thomas

谢谢!
托马斯

Update 1:

更新 1:

Here is what I have tried so far.

这是我迄今为止尝试过的。

1.) I added UIImageView to my xib.
2.) Connect the following outlet to the UIImageView in IB

1.) 我将 UIImageView 添加到我的 xib 中。
2.) 将下面的outlet连接到IB中的UIImageView

IBOutlet UIImageView *cameraWindow;

IBOutlet UIImageView *cameraWindow;

3.) I put the following code in viewWillAppear

3.) 我把下面的代码放在viewWillAppear

-(void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];
 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
 picker.delegate = self;

 picker.sourceType = UIImagePickerControllerSourceTypeCamera;
 [self presentModalViewController:picker animated:YES];

 NSLog(@"viewWillAppear ran");
}

But this method does not run, as evident by the absence of NSLog statement from my console. Please help!

但是这个方法没有运行,从我的控制台中没有 NSLog 语句就可以看出这一点。请帮忙!

Thanks,
Thomas

谢谢,
托马斯



Update 2:

更新 2:

OK I got it to run by putting the code in viewDidLoadbut my camera still doesn't show up...any suggestions? Anyone....? I've been reading the UIImagePickerController class reference, but am kinda unsure how to make sense of it. I'm still learning iPhone, so it's a bit of a struggle. Please help!

好的,我通过将代码放入viewDidLoad让它运行,但我的相机仍然没有显示......有什么建议吗?任何人....?我一直在阅读UIImagePickerController 类参考,但我有点不确定如何理解它。我还在学习 iPhone,所以这有点困难。请帮忙!

- (void)viewDidLoad 
{ 
 [super viewDidLoad]; 

 // Create a bool variable "camera" and call isSourceTypeAvailable to see if camera exists on device
 BOOL camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

 // If there is a camera, then display the world throught the viewfinder
 if(camera)
 { 
  UIImagePickerController *picker = [[UIImagePickerController alloc] init];

  // Since I'm not actually taking a picture, is a delegate function necessary?
  picker.delegate = self;

  picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  [self presentModalViewController:picker animated:YES];

  NSLog(@"Camera is available");
 }

 // Otherwise, do nothing.
 else 
  NSLog(@"No camera available");
}

Thanks!
Thomas

谢谢!
托马斯



Update 3:

更新 3:

A-HA! Found this on the Apple Class Reference.

啊哈!在 Apple Class Reference 上找到了这个。

Discussion

The delegate receives notifications when the user picks an image or movie, or exits the picker interface. The delegate also decides when to dismiss the picker interface, so you must provide a delegate to use a picker. If this property is nil, the picker is dismissed immediately if you try to show it.

讨论

当用户选择图像或电影,或退出选择器界面时,委托会收到通知。委托还决定何时关闭选择器界面,因此您必须提供一个委托才能使用选择器。如果此属性为零,则选择器会在您尝试显示时立即关闭。

Gonna play around with the delegate now. Then I'm going to read on wtf a delegate is. Backwards? Whatever :-p

现在要和代表一起玩。然后我将阅读代表的wtf。向后?随便 :-p



Update 4:

更新 4:

The two delegate functions for the class are – imagePickerController:didFinishPickingMediaWithInfo: – imagePickerControllerDidCancel:

该类的两个委托函数是 – imagePickerController:didFinishPickingMediaWithInfo: – imagePickerControllerDidCancel:

and since I don't actually want to pick an image or give the user the option to cancel, I am just defining the methods. They should never run though....I think.

并且由于我实际上不想选择图像或给用户取消的选项,我只是定义方法。他们永远不应该跑……我想。

回答by Tharindu Madushanka

add

添加

[picker dismissModelViewControllerAnimated:YES];

[选择器dismissModelViewControllerAnimated:YES];

to delegate method bodies.

委托方法体。

It will dismiss the view.

它将忽略该视图。