ios - 在 View Controller 生命周期中何时呈现 UIImagePickerController?

标签 ios iphone uiviewcontroller uiimagepickercontroller modalviewcontroller

当用户在我的应用程序中点击相机选项卡来拍照时,我想立即呈现一个 UIImagePickerController (类似于 Instagram 等许多拍照应用程序)。目前,我有一个名为 CameraViewController 的 VC,它显示从 UIImagePickerController 拍摄的图片,并在 CameraViewControllerviewWillAppear 函数中显示 ImagePickerController 模态 当我检测到没有拍摄照片时。

虽然这在大多数情况下都有效,但我注意到有时我会收到一个异常消息:

“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘应用程序试图以模态方式呈现事件 Controller 。’

根据此处一些 SO 问题的建议,我还尝试在 viewDidAppear 中呈现 Controller 。但是,对于 viewDidAppear,模态 ImagePicker 永远不会被关闭,而且我不断收到错误“对开始/结束外观转换的调用不平衡。”

我的问题是:

  1. 我目前以模态方式呈现 ImagePickerController 以供用户拍照,然后在拍照后将其关闭。这通常是最可靠/最无问题的 ImagePickerController 的处理方式吗。
  2. 我还应该考虑将 ImagePickerController 放在 View 生命周期中的什么地方才能获得所需的效果?

这是我的代码:

viewWillAppear

BOOL modalPresent = (BOOL)(self.presentedViewController);
if (!appDelegate.imageStorageDictionary[@"picture1"]){
    if (modalPresent == NO){
        [self presentViewController:self.imagePickerController animated:NO completion:nil];
    }
}

imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

//Other lines omitted 
if (![[self imagePickerController] isBeingDismissed]) 
    [self dismissViewControllerAnimated:NO completion:nil];

最佳答案

我不太了解你关于 -viewWillAppear 的问题,但你必须知道一件事 -viewWillAppear 会被多次调用。原因是,第一个调用将是当您的 viewController 被加载并且 viewDidLoad 和 appear 将像往常一样被调用,其次是当您呈现一个 modalViewController (imagePickerController) 当它再次被关闭时 viewWillAppear 将被调用,因为 viewController 的 View 即将出现..

其次,关于您希望在用户点击相机按钮时显示相机的功能,请执行以下操作:

  if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
     if(myPicker)
     {
        [myPicker release];
         myPicker = nil;
      }
 myPicker   = [[UIImagePickerController alloc] init];

     [self presentViewController:myPicker animated:YES completion:NULL];
}
else
{
    UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    altnot.tag=103;
    [altnot show];
    [altnot release];
}

用于选择已经捕获的图像..

-(void)选择已经拍摄的照片 {

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
    if(myPicker)
    {
       [myPicker release];
       myPicker = nil;
    }
    myPicker          = [[UIImagePickerController alloc] init];
    myPicker.delegate = self;
    myPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    myPicker.allowsEditing = NO;

}
[self presentViewController:myPicker animated:YES completion:NULL];

注意:我每次使用的时候都释放了之前分配的picker对象,但是没有必要。您可以使用我的选择器的唯一单个实例,不要将其标记为错误。并且在 viewController 中使用 imagePicker-controller 绝对没问题,它不会造成伤害。次,然后 viewWill 出现也会被调用,每次你关闭它。

关于ios - 在 View Controller 生命周期中何时呈现 UIImagePickerController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21373552/

相关文章:

ios - UIViewController, Storyboard,没有 init 方法

ios - 在 Swift 中使用 Firebase 获取值属性

ios - Swift 尝试将 TableView 单元格上的点击事件链接到 Controller 方法

ios - AdMob 横幅在 iOS9 中不起作用

ios - 刷新 UINavigationController 内的 "root"数据并更新导航堆栈中的数据

IOS- NSNotificationCenter- 通知特定实例

ios - 如何在 SwiftUI 中使用 DatePicker 创建警报

ios - 移动行在索引路径 : how to delete section once last row is moved to another section

ios - iPhone UI 元素——自定义还是默认?

ios - iOS8.0中UITableViewRowAction如何管理等宽?(更多,删除等操作)