在UIImagePickerController之后切换视图

时间:2012-05-22 10:10:42

标签: objective-c ios uiimagepickercontroller

当用户完成选择图像时,我正在尝试将我的应用切换到另一个视图,但是当用户完成选择图像时会发生这种情况,它会取消选择器并返回到相同的视图。
这是我的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissModalViewControllerAnimated:YES];

    DrawingViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Drawing"];
    viewController.image.image = image;
    [self presentModalViewController:viewController animated:NO];
}

1 个答案:

答案 0 :(得分:6)

之所以发生这种情况,是因为您正在使用动画解雇拾取器模态视图控制器并立即显示新的视图控制器。在上一个模态视图控制器被解除后,您只能呈现新的模态视图控制器。 解决方案是在没有动画的情况下解除拾取器模态:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissModalViewControllerAnimated:NO];

    DrawingViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Drawing"];
    viewController.image.image = image;
    [self presentModalViewController:viewController animated:NO];
}

另一种解决方案(仅适用于iOS 5 )是使用dismissViewControllerAnimated: completion:方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
   [picker dismissViewControllerAnimated:YES completion:^(){
        DrawingViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Drawing"];
        viewController.image.image = image;
        [self presentModalViewController:viewController animated:NO];
    }];


}