在 viewDidAppear 中无限调用 presentViewController

Posted

技术标签:

【中文标题】在 viewDidAppear 中无限调用 presentViewController【英文标题】:presentViewController called infinitely in viewDidAppear 【发布时间】:2017-12-06 23:47:29 【问题描述】:

我正在尝试在我的应用中实现全屏 UIImagePickerController。我无法在viewDidLoad 中显示视图控制器,因为presenting view controllers on detached view controllers is discouraged。但是,我的viewDidAppear 被无限调用,并且图像选择器控制器被添加,然后在每次调用时从屏幕上掉下来。我尝试分派到主队列,但这并没有解决问题。

- (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];

    ipc = [[UIImagePickerController alloc] init];
    ipc.delegate = self;


    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    
        ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
        ipc.showsCameraControls = NO;

        CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); 
        ipc.cameraViewTransform = translate;

        CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
        ipc.cameraViewTransform = scale;

        ipc.showsCameraControls = NO;
        ipc.tabBarController.tabBar.hidden = YES;
        ipc.allowsEditing = NO;
        [self presentViewController:ipc animated:YES completion:nil];

    



#pragma mark - ImagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

    UIImage* theImage = [info objectForKey:UIImagePickerControllerOriginalImage];

    if( picker.sourceType == UIImagePickerControllerSourceTypeCamera )
    
        UIImageWriteToSavedPhotosAlbum(theImage, nil, nil, nil);
    
    int height = -1;
    if([[NSUserDefaults standardUserDefaults] integerForKey:@"reduce_image"] == 0)
        height = 640;
     else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"reduce_image"] == 1) 
        height = 1024;
     else 
        height = 1600;
    

    UIImage* resizedImageForUpload = [UtilityFunctions scaleAndRotateImage:theImage maxResolution:height];
    NSData* imageDataForUpload = UIImageJPEGRepresentation(resizedImageForUpload, 1);   // reduced image! //

    NSString *userDataset = [UtilityFunctions retrieveFromUserDefaults:@"dataset"];

    [self didPickImage:imageDataForUpload atLocation:currentLocation
                     userDataset: userDataset];

    [picker dismissViewControllerAnimated:YES completion:nil];

    [mLocationManager stopUpdatingLocation];



-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    /*navigate to home tab*/
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.tabBarController.tabBar.hidden = NO;
    [self.tabBarController setSelectedIndex:0];


【问题讨论】:

在我看来问题出在代码的其他地方。你在 ipc 的委托方法中做什么? 使用委托方法更新:) 在您的选择器dismissViewControllerAnimated 上设置断点可能会有所帮助。我认为它被意外解雇了。 @mschmidt 你是对的!我关闭了viewWillDisappear 中的图像选择器控制器,这就是让我们进入这个循环的原因! 不相关,但为什么要先创建图像选择器,然后检查源类型是否可用?那是倒退。 【参考方案1】:

您正在关闭图像选择器视图控制器以响应其委托方法被调用,该方法又调用您的 -viewDidAppear: 方法,该方法只是呈现另一个图像选择器视图控制器。您需要以某种方式打破循环,使用您编写的代码,您描述的行为是我所期望的。

一种方法是将ipc 设置为-viewDidLoad 中的UIImagePickerController 实例(仅调用一次),如果它在-viewDidAppear: 中不是nil,则呈现它,然后nil它在图像选择器委托方法中显示,下次调用 -viewDidAppear: 时不会重新呈现它。

【讨论】:

imagePickerControllerDidCanceldidFinishPickingMediaWithInfo 中取消它?

以上是关于在 viewDidAppear 中无限调用 presentViewController的主要内容,如果未能解决你的问题,请参考以下文章

viewWillAppear 调用但 viewDidAppear 在 pushViewController 之后未调用

在 IOS 4.x 或更低版本中,将子视图添加到视图时不会调用 viewDidAppear 方法,为啥?

何时实际调用 viewDidLoad / viewDidAppear? [复制]

当我在 iOS 4.3 中调用 ViewDidAppear 时,它不起作用?

关闭模式视图时未调用 ViewDidAppear

Viewwillappear 已调用但 viewdidappear 未调用