为什么我无法让相机显示?

发布于 2024-11-05 01:08:29 字数 604 浏览 0 评论 0原文

遵循几个教程中的这个简单代码,所有这些都非常相似,但我一生都无法让它做除了显示空白 UIViewController 之外的任何事情。 self.imagePicker 在 .h 文件中定义,给定一个属性,然后正常合成:

- (void)viewDidLoad {



    // Create image picker controller
    self.imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    self.imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

    // Delegate is self
    //imagePicker.delegate = self;

    NSLog(@"%@", self.imagePicker);

    // Show image picker
    [self presentModalViewController:self.imagePicker animated:YES];

    [super viewDidLoad];
}

Followed this simple code from several tutorials, all of which are very similar, but I cant for the life of me get this to do anything other than show a blank UIViewController. self.imagePicker is defined in the .h file, given a property, and then synthesized as normal:

- (void)viewDidLoad {



    // Create image picker controller
    self.imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    self.imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

    // Delegate is self
    //imagePicker.delegate = self;

    NSLog(@"%@", self.imagePicker);

    // Show image picker
    [self presentModalViewController:self.imagePicker animated:YES];

    [super viewDidLoad];
}

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

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

发布评论

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

评论(3

苯莒 2024-11-12 01:08:34
self.imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

您正在将选取器设置为库,您需要将其设置为相机。

self.imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

编辑:但是,这并不能解释为什么您会得到空白视图。

在呈现选择器之前尝试调用[super viewDidLoad]

self.imagePicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

You are setting the picker to the library, you need to set it to the camera.

self.imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;

EDIT: However, this doesn't explain why you'd get a blank view.

Try calling [super viewDidLoad] before presenting the picker.

时光与爱终年不遇 2024-11-12 01:08:34

此外,设置通过 init 调用保留的属性会导致自动内存泄漏。

这是因为您最终的保留计数为 2。一种用于 init,一种用于保留。创建一个变量,将其分配给您的属性,然后释放它。

仅供参考。

Also, setting a property that retains with an init call is an automatic memory leak.

This is because you end up with a retain count of two. One for the init and one for the retain. Create a variable, assign it to your property and then release it.

Just an FYI.

她比我温柔 2024-11-12 01:08:33

尝试在 viewDidLoad 之外的其他位置加载 UIImagePickerView。过去,当您在另一个视图控制器的 viewDidload 方法中呈现视图控制器时,我见过诸如空白 UIViewController 和其他古怪的 ViewController 行为之类的情况。也许可以尝试使用 UIViewController 方法 viewDidAppear 来代替。如果不起作用,请将其放入一个完全独立的方法中,在加载呈现的视图控制器后,您会稍微延迟地调用该方法。

Try loading your UIImagePickerView in somewhere other than viewDidLoad. In the past I have seen things like blank UIViewController's and other wacky ViewController behavior when you present a viewcontroller within the viewDidload method of another. Perhaps try the UIViewController method viewDidAppear instead. If doesn't work, put it in an entirely seperate method that you call with a slight delay once the presenting viewcontroller has been loaded.

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