presentViewController在viewDidAppear

时间:2017-12-06 23:47:30

标签: ios objective-c uiimagepickercontroller presentviewcontroller viewdidappear

我正在尝试在我的应用中实现全屏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];

}

1 个答案:

答案 0 :(得分:0)

您正在关闭图像选择器视图控制器,以响应其调用的委托方法,该方法又调用您的Stop方法,该方法只显示另一个图像选择器视图控制器。你需要以某种方式打破周期,用你编写的代码描述你所描述的行为是我期望的。

一种方法是将-viewDidAppear:设置为ipcUIImagePickerController的实例(仅调用一次),如果它是非{{ 1}}在-viewDidLoad中,然后nil在图片选择器委托方法中,它在下次-viewDidAppear:被调用时不会重新呈现。