点击时使UIViewController成为UIImagePicker

时间:2012-07-17 04:15:44

标签: objective-c ios uiimagepickercontroller uitabbar

我有一个使用UITabBarController的应用,其中一个标签应该是完整的相机视图(类似于Instagram)。当我启动应用程序并转到该视图时,大部分屏幕都是空白的,并且没有加载图像库选取器/摄像机视图。任何帮助将不胜感激。

这是我的viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    imagePicker = [[UIImagePickerController alloc] init];

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    } else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }

    [imagePicker setDelegate:self];

    [imagePicker setEditing:NO];

    // Place image picker (camera viewing angle on the screen)
    [self presentViewController:imagePicker animated:YES completion:nil];
}

1 个答案:

答案 0 :(得分:0)

尝试将该代码放在viewDidAppear中 - 这对我有用。更好的是,将最后一行放在viewDidLoad中,所以它只调用一次,然后将最后一行放在viewDidAppear方法中。当您取消图像选择器(我测试的唯一内容)时,它会重新出现,因为将再次调用viewDidAppear,因此您需要实现imagePickerControllerDidCancel:这样可以防止这种情况发生:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
    self.imagePicker = nil;
}

并且,在你的viewDidAppear中,在尝试呈现它之前,输入一个if语句来查看选择器是否存在:

if (self.imagePicker) {
    [self presentViewController:self.imagePicker animated:YES completion:nil];
    }