【问题标题】:Problem opening new ViewController after UIImagePickerController在 UIImagePickerController 之后打开新 ViewController 的问题
【发布时间】:2011-06-27 14:35:38
【问题描述】:

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

不幸的是,我似乎可以打开 UIImagePickerController 模态视图,或者切换到 UnprocessedPhotoViewController 作为模态视图,但不能同时按顺序进行。

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

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

保证 ImagePicker modal 关闭然后打开 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];
}

【问题讨论】:

    标签: iphone uiviewcontroller uiimagepickercontroller


    【解决方案1】:

    在你的根视图控制器中实现 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];
        }
    }
    

    【讨论】:

    • 我刚刚发布了一个替代解决方案,该解决方案避免了对标志变量和覆盖 viewDidAppear 的需要:
    【解决方案2】:

    另一种方法是使用您自己的动画包装内置的解雇动画,然后捕获 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];
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 2015-11-10
      • 2017-05-25
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多