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

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

问题描述

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

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)动画允许相机启动,但应用程序立即崩溃,最后一个例程是[UIWindowController transitionViewDidComplete:fromView:toView]; (由调试器引用)

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属性的方式。如果它是使用retain而不是assign来声明的,那么除非你在某个地方明确地释放它,所以picker将始终存在于内存中。在这种情况下,您应该在将init'd对象分配给属性时自动释放它。

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 Camera?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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