在UIImagePickerController之后打开新的ViewController时出现问题

时间:2011-02-15 13:16:22

标签: iphone uiviewcontroller uiimagepickercontroller

我试图在我的UIImagePickerController的委托函数返回“didFinishPickingImage”后立即打开一个新视图(UnprocessedPhotoViewController)。

不幸的是,似乎我可以打开UIImagePickerController模式视图,或者切换到UnprocessedPhotoViewController作为模态,但不是两个顺序。

在下面的代码中,按下按钮可激活pickPhoto IBAction。此代码成功激活UIImagePickerController。在用户选择图像之后,调用didFinishPickingImage委托函数,该函数将图像存储到变量,尝试关闭UIImagePickerController模式并在新模态中打开UnprocessedPhotoViewController。

注意:如果我注释掉ImagePicker并直接运行“showPhoto”,则UnprocessedPhotoViewController会成功显示。此外,如果我创建一个新按钮来启动任一视图它成功运行,但我无法按顺序启动视图。我希望在用户选择图像后,将启动新视图,以便用户处理图像。

保证ImagePicker模式关闭然后打开UnprocessedPhotoViewController的正确方法是什么?

感谢!!!

代码:

- (IBAction)pickPhoto:(id)sender{

//TODO: To be replaced with the gallery control launching code

// Load Image Selection Code    
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imgPicker animated:YES];

}



// Dummy function assumes you either picked (or took a picture =D) of Angie and moves you right to the unprocessed viewing screen.
- (void) showPhoto{

// Start new view controller
[self dismissModalViewControllerAnimated:YES];
UnprocessedPhotoViewController *upViewController = [[UnprocessedPhotoViewController alloc] initWithNibName:@"UnprocessedPhotoViewController" bundle:nil];
upViewController.imageView.image = selectedImage;
upViewController.delegate = self;
upViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:[upViewController animated:YES]];
[upViewController release];

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img {
selectedImage = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[self dismissModalViewControllerAnimated:YES];

[self showPhoto];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

2 个答案:

答案 0 :(得分:4)

在根视图控制器中实现viewDidAppear,并根据成员数据决定是否调用showPhoto。以下示例只是重新呈现UIImagePicker,但任何新的模态视图都可以使用。每次出现根视图控制器的视图时都会调用viewDidAppear,因此您必须确保在调用上下文时已知该上下文。但这是确定模态视图控制器消失的确定性方法。

- (IBAction) showPicker: (id) sender
{
    UIImagePickerController* picker = [[[UIImagePickerController alloc] init] autorelease];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    picker.allowsEditing = YES;

    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imageChosen = YES;

    [self dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    imageChosen = NO;

    [self dismissModalViewControllerAnimated:YES];
}

- (void) viewDidAppear: (BOOL) animated
{
    if ( imageChosen )
    {
        [self showPicker: self];
    }
}

答案 1 :(得分:3)

另一种方法是使用您自己的动画包装内置的解雇动画,然后捕捉animationDidStop“事件”。这将创建一个复合动画,因此当内置动画完成后,您的(空)包装动画将结束您并提醒您已完成。

这比IMO中的其他答案稍微清晰一点,因为你不需要保留状态变量或覆盖viewDidAppear:(在我的应用程序中,呈现选择器的视图控制器被移除了很多对象从处理选择器管理的实用程序代码,这意味着任何使用我的共享实用程序的视图控制器都必须覆盖viewDidAppear:,否则无法工作):

-(void)dismissalAnimationDone:(NSString*)animationID 
                     finished:(BOOL)finished context:(void*)context
{
    UIImage* image = (UIImage*) context;
    // present controllers as you please
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [UIView beginAnimations:@"dismissal wrapper" context:image];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(dismissalAnimationDone:finished:context:)];

    [self.delegate dismissModalViewControllerAnimated:YES];

    [UIView commitAnimations];
}