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];
}
dismissViewControllerAnimated. I think it is dismissed by accident.viewWillDisappear, which is what got us into this loop!