如何让 iPhone 上的摄像头出现在应用程序委托中?是否可以?

发布于 2024-09-05 06:44:30 字数 887 浏览 2 评论 0原文

我只是在玩一个打开相机的简单程序。这就是我想做的全部事情。我是一名初学者,我相信我已经掌握了 iPhone 用户界面管理的基础知识,所以我决定尝试一下。

我现在想做的是......

- (BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    UIImagePickerController * camera = [[UIImagePickerController alloc] init];
    camera.delegate = self;
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;
    camera.allowsEditing = NO;
    camera.showsCameraControls = NO;
    [viewController presentModalViewController:camera animated:NO];

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

return YES:
}

所以基本上,初始化相机,设置一些东西并将其显示在主视图中。我将相机的委托设置为 self,因为此代码放置在委托类中(是的,委托类符合 UIImagePickerControllerDelegate && UINavigationControllerDelegate)。

现在的主要问题是屏幕上什么也没有出现。我完全不知道我做错了什么,特别是因为程序正在正确构建,没有错误或警告......

任何帮助表示赞赏!非常感谢:D

I'm just playing around with a simple program that opens the camera. That's literally all that I want to do. I'm a beginner and I believe that I have the basics down in terms of UI management for the iPhone so I decided to give this one a whirl.

What I'm trying to do right now is...

- (BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    UIImagePickerController * camera = [[UIImagePickerController alloc] init];
    camera.delegate = self;
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;
    camera.allowsEditing = NO;
    camera.showsCameraControls = NO;
    [viewController presentModalViewController:camera animated:NO];

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

return YES:
}

So basically, initialize the camera, set some things and show it in the main view. I set the camera's delegate to self because this code is placed in the delegate class (and yes, the delegate class is conforming to UIImagePickerControllerDelegate && UINavigationControllerDelegate).

Main problem right now is that nothing is appearing on the screen. I have absolutely no idea what I'm doing wrong, especially since the program is building correctly with no errors or warnings...

Any help is appreciated! Thanks a lot :D

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

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

发布评论

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

评论(1

一袭白衣梦中忆 2024-09-12 06:44:30

在将 UIImagePickerController 的视图添加到窗口之前,您正尝试从视图控制器显示 UIImagePickerController。那是行不通的。它必须从已经显示的视图中呈现。

我不确定这是否有效,因为我还没有尝试过,但请切换代码的​​顺序。首先将视图控制器添加到窗口:

- (BOOL) application:(UIApplication*) application 
             didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    UIImagePickerController * camera = [[UIImagePickerController alloc] init];
    camera.delegate = self;
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;
    camera.allowsEditing = NO;
    camera.showsCameraControls = NO;
    [viewController presentModalViewController:camera animated:NO];

    return YES:
}

如果这不起作用,请尝试将 UIImagePickerController 代码移动到根视图控制器的 -viewDidLoad(无论代码中的 viewController 是什么)。

You are trying to display the UIImagePickerController from your view controller before you'e added its view to the window. That won't work. It has to be presented from a view that is already displaying.

I'm not absolutely sure this will work as I haven't tried it, but switch the order of your code. Add your view controller to the window first:

- (BOOL) application:(UIApplication*) application 
             didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    UIImagePickerController * camera = [[UIImagePickerController alloc] init];
    camera.delegate = self;
    camera.sourceType = UIImagePickerControllerSourceTypeCamera;
    camera.allowsEditing = NO;
    camera.showsCameraControls = NO;
    [viewController presentModalViewController:camera animated:NO];

    return YES:
}

If this doesn't work, then try moving your UIImagePickerController code to your -viewDidLoad of your root view controller (whatever viewController is in your code).

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