如何在 viewDidLoad 上启动 iPhone 相机? [英] How to launch iPhone Camera on viewDidLoad?

查看:22
本文介绍了如何在 viewDidLoad 上启动 iPhone 相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加载视图时似乎无法启动相机.我最终让用户必须找到并按下屏幕上的按钮才能加载相机(冗余).我怎样才能做到这一点?代码如下:

I can't seem to launch the camera when loading my view. I end up making the user have to find and press a button on the screen just to load the camera (redundant). How can I do this? Code follows:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.allowsImageEditing = NO;
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [super viewDidLoad];

    [self presentModalViewController:self.imgPicker animated:YES];

}

更新:
将上述代码放入 -viewDidAppear:(BOOL)animated 允许启动相机,但应用程序随即崩溃,最后一个例程为 [UIWindowController transitionViewDidComplete:fromView:toView];(由 Debugger 引用)

UPDATE:
placing the above code into -viewDidAppear:(BOOL)animated allowed the camera to be launched, but the app crashed immediately afterward with the last routine being [UIWindowController transitionViewDidComplete:fromView:toView]; (as cited by Debugger)

推荐答案

如果第一个不起作用,您应该在 viewWillAppear: 或 viewDidAppear: 中进行.尝试在 viewDidLoad 中执行此操作将不起作用,因为在首次创建视图后调用该视图,并且此时该视图不是其他任何内容的子视图.据我了解,为了在 self 上调用 presentModalViewController,视图必须在某个级别显示在 UIWindow 中.

You should do it in viewWillAppear:, or viewDidAppear: if the first doesn't work. trying to do it in viewDidLoad won't work because that is called after the view is first created, and the view isn't a subview of anything else at that point. As far as i understand it, in order to call presentModalViewController on self, the view must at some level be displayed in the UIWindow.

我刚刚注意到的另一件事;您的代码泄漏内存取决于您如何声明 imgPicker 属性.如果它是用保留而不是赋值声明的,那么除非你在某个地方明确释放它两次,否则选择器将始终存在于内存中.在这种情况下,您应该在将初始化对象分配给属性时自动释放它.

One other thing i just noticed; your code leaks memory depending on how you declare your imgPicker property. if it is declared with retain instead of assign, then unless you explicitly release it twice somewhere that picker will always exist in memory. You should autorelease the init'd object as you assign it to the property in that case.

这篇关于如何在 viewDidLoad 上启动 iPhone 相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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