如何使用 UIImagePickerController 只显示相机而不拍照?

发布于 2024-08-27 23:48:45 字数 2355 浏览 2 评论 0原文

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

谢谢!
Thomas

更新 1:

这是我到目前为止所尝试过的。

1.) 我将 UIImageView 添加到我的 xib 中。
中的 UIImageView

2.) 将以下插座连接到 IB IBOutlet UIImageView *cameraWindow

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

但是此方法未运行,从我的控制台中缺少 NSLog 语句可以明显看出。请帮忙!

谢谢,
Thomas


更新 2:

好的,我通过将代码放入 viewDidLoad 来运行它,但我的相机仍然没有显示......有什么建议吗?有人吗……?我一直在阅读 UIImagePickerController 类参考< /a>,但我有点不确定如何理解它。我还在学习 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");
}

谢谢!
托马斯


更新 3:

啊哈!在 Apple 类参考中找到了这个。

讨论

代表收到通知 当用户选择图像或电影时, 或退出选择器界面。这 代表还决定何时解散 选择器界面,所以你必须 提供一个委托来使用选择器。如果 该属性为零,选择器是 如果您尝试立即解雇 显示它。

现在要和代表一起玩了。然后我要读一下代表是什么。向后?不管怎样 :-p


更新 4:

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

由于我实际上不想选择图像或为用户提供取消选项,所以我只是定义方法。但他们不应该逃跑……我想。

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:

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

IBOutlet UIImageView *cameraWindow;

3.) I put the following code in 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!

Thanks,
Thomas


Update 2:

OK I got it to run by putting the code in viewDidLoad but 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!

- (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:

A-HA! Found this on the 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


Update 4:

The two delegate functions for the class are
– 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2024-09-03 23:48:45

添加

[选择器
missModelViewControllerAnimated:YES];

委托方法体。

它将驳回该视图。

add

[picker
dismissModelViewControllerAnimated:YES];

to delegate method bodies.

It will dismiss the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文