为什么我无法让相机显示?
遵循几个教程中的这个简单代码,所有这些都非常相似,但我一生都无法让它做除了显示空白 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在将选取器设置为库,您需要将其设置为相机。
编辑:但是,这并不能解释为什么您会得到空白视图。
在呈现选择器之前尝试调用
[super viewDidLoad]
。You are setting the picker to the library, you need to set it to the camera.
EDIT: However, this doesn't explain why you'd get a blank view.
Try calling
[super viewDidLoad]
before presenting the picker.此外,设置通过 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.
尝试在
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 methodviewDidAppear
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.