【发布时间】:2010-04-08 14:32:34
【问题描述】:
我想知道如何在预定义的帧(而不是整个屏幕)内打开相机。当视图加载时,我有一个盒子,在里面,我想显示相机看到的东西。我不想拍照,只是基本上将相机用作取景器。我已经搜索了这个网站,但还没有找到我要找的东西。请帮忙。
谢谢!
托马斯
更新 1:
这是我迄今为止尝试过的。
1.) 我将 UIImageView 添加到我的 xib。
2.) 将以下插座连接到 IB 中的 UIImageView
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 语句就可以看出这一点。请帮忙!
谢谢,
托马斯
更新 2:
好的,我通过将代码放入 viewDidLoad 来运行它,但我的相机仍然没有显示...有什么建议吗?任何人....?我一直在阅读UIImagePickerController class reference,但我有点不确定如何理解它。我还在学习 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 类参考中找到了这个。
讨论
代理接收通知 当用户选择图像或电影时, 或退出选择器界面。这 代表还决定何时解雇 选择器界面,所以你必须 提供委托以使用选择器。如果 此属性为 nil,选择器为 如果您尝试立即解雇 显示出来。
现在要和代表一起玩了。然后我将阅读 wtf a delegate is。向后?随便:-p
更新 4:
该类的两个委托函数是 – imagePickerController:didFinishPickingMediaWithInfo: – imagePickerControllerDidCancel:
因为我实际上并不想选择图像或让用户选择取消,所以我只是在定义方法。他们永远不应该跑……我想。
【问题讨论】:
标签: objective-c iphone xcode iphone-sdk-3.0 uiimagepickercontroller