I have a UIImagePickerController with a custom overlay view. The view controller opens originally with the camera as the source type.But there is a button that switches the source type to photo album. If they enter the album mode, they can hit cancel
to switch the source type back to camera.
This works for the first round. But if they press the album button a second time (after having already entered album mode and canceled out of it), the screen loads the white background but doesn't load the user's photo library. The app is then stuck.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"CameraView" owner:self options:nil];
self.overlayView.frame = ipc.cameraOverlayView.frame;
ipc.cameraOverlayView = self.overlayView;
self.overlayView = nil;
CGSize screenBounds = [UIScreen mainScreen].bounds.size;
int cameraViewHeight;
int adjustedYPosition;
/*Center Uiimagepickercontroller in screen */
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
cameraViewHeight = screenBounds.width * 1.333;
adjustedYPosition = (screenBounds.height - cameraViewHeight) / 2;
NSLog(@"portrait screenbounds width: %f, screenbounds height: %f", screenBounds.width, screenBounds.height);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
ipc.cameraViewTransform = translate;
NSLog(@"in portrait value is %d", adjustedYPosition);
}else{
//landscape mode
cameraViewHeight = screenBounds.height * 1.333;
adjustedYPosition = (screenBounds.width - cameraViewHeight) / 2;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
ipc.cameraViewTransform = translate;
}
ipc.showsCameraControls = NO;
ipc.tabBarController.tabBar.hidden = YES;
}else{
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentViewController:ipc animated:YES completion:nil];
[_activityIndicator stopAnimating];
}
- (IBAction)albumView:(id)sender {
[ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
/*navigate to previous tab */
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
[picker dismissViewControllerAnimated:YES completion:nil];
LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
[self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
}else{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
}
I'm not sure why this works, so if someone can explain it I will mark their response as correct. But this is my solution:
- (IBAction)albumView:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker.view setFrame:self.view.frame];
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagePicker setDelegate:self];
[ipc presentViewController:imagePicker animated:YES completion:nil];
}
- (IBAction)cancel_IPC:(id)sender {
[self imagePickerControllerDidCancel:ipc];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
[self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* origImage = [info objectForKey:UIImagePickerControllerOriginalImage];
/*do whatever you need to do with the img*/
[picker dismissViewControllerAnimated:YES completion:nil];
/*dismiss the original UIImagePickerController in background */
if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
[ipc dismissViewControllerAnimated:YES completion:nil];
}
[mLocationManager stopUpdatingLocation];
}