如何在viewLoad中加载UIImagePickerController

时间:2009-11-19 02:21:55

标签: uiimagepickercontroller viewdidload

这似乎是一个非常简单的想法......我有一个按钮,上面写着“加载图片”。单击该按钮时,我想删除当前视图,加载新视图,打开图像选择器,获取所选图片,并在刚刚加载的视图中显示它。我已经阅读了几个文件,说UIImagePicker在viewDidLoad中不起作用,并且发现一个建议使用awakeFromNib的线程,但即使这样也行不通......这里是代码。请帮忙。

在MenuViewController中

-(IBAction)loadPictureButtonPressed
{
    UIView *parent = [self.view superview];
    if(self.loadPictureViewController.view.superview == nil)
    {
         if(self.loadPictureViewController == nil)
         {
              LoadPictureViewController *picController = [[LoadPictureViewController alloc]
          initWithNibName:@"LoadPictureView" bundle: nil];
              self.loadPictureViewController = picController;
              [picController release];
        }
    }
    [parent addSubview:self.loadPictureViewController.view];
    [self.view removeFromSuperview];
}

在LoadPictureViewController中

-(void)awakeFromNib
{
     if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
     {  
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentModalViewController:picker animated:YES];
            [picker release];
    }
    else
    {
            UIAlertView *alert = [[UIAlertView alloc]
                           initWithTitle:@"Error!"
                           message:@"Device does not support photo library";
                           delegate:nil
                           cancelButtonTitle:@"Ok"
                           otherButtonTitles:nil];
           [alert show];
           [alert release];
   }
}

我再次尝试将此代码放在viewDidLoad和awakeFromNib中。请帮忙

2 个答案:

答案 0 :(得分:0)

我只是在viewDidLoad中设置NSTimer,定时器为0,以选择我加载它的方法。

[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(camera) userInfo:nil repeats:NO];

答案 1 :(得分:0)

您可能希望在-viewDidLoad中使用以下内容,而不是NSTimer,

[self performSelector:@selector(camera) withObject:nil afterDelay:0];