0

I am trying to implement a fullscreen UIImagePickerController in my app. I couldn't present the view controller in viewDidLoad because presenting view controllers on detached view controllers is discouraged. However, my viewDidAppear gets called infinitely and the image picker controller gets added and then drops from the screen with each call. I tried dispatching to the main queue, but that did not resolve the issue.

- (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];

}
6
  • It seems to me the problem is elsewhere in your code. What are you doing in ipc's delegate methods? Commented Dec 6, 2017 at 23:54
  • updated with delegate methods :) Commented Dec 7, 2017 at 0:00
  • It might help to set a breakpoint on your pickers dismissViewControllerAnimated. I think it is dismissed by accident. Commented Dec 7, 2017 at 0:17
  • @mschmidt you are right! I dismiss the image picker controller in viewWillDisappear, which is what got us into this loop! Commented Dec 7, 2017 at 0:20
  • Unrelated but why do you first create the image picker and then check to see if the source type is available? That's backwards. Commented Dec 7, 2017 at 0:20

1 Answer 1

0

You're dismissing the image picker view controller in response to its delegate method being called, which in turn calls your -viewDidAppear: method, which just presents another image picker view controller. You need to break the cycle somehow, with the code you've written the behavior you described is what I'd expect.

One approach would be to set ipc to an instance of UIImagePickerController in -viewDidLoad (which is only called once), the present it if it's non-nil in -viewDidAppear:, then nil it out in the image picker delegate methods to it's not re-presented the next time -viewDidAppear: is called.

Sign up to request clarification or add additional context in comments.

1 Comment

nil it out in imagePickerControllerDidCancel and didFinishPickingMediaWithInfo ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.