【问题标题】:presentViewController called infinitely in viewDidAppear在 viewDidAppear 中无限调用 presentViewController
【发布时间】: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 中的图像选择器控制器,这就是让我们进入这个循环的原因!
  • 不相关,但为什么要先创建图像选择器,然后检查源类型是否可用?那是倒退。

标签: ios objective-c uiimagepickercontroller presentviewcontroller viewdidappear


【解决方案1】:

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

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

【讨论】:

  • imagePickerControllerDidCanceldidFinishPickingMediaWithInfo 中取消它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
相关资源
最近更新 更多