Search code examples
iosiphoneobjective-cuiimagepickercontroller

Landscape orientation image picker.


I'm creating an app that is landscape only, it uses an image picker control.

Looking through the site I've discovered that apple only allow portrait for this for some reason. I'm okay with it flipping to portrait for this one section, if it means the user can select a photo from the library. Below is my code that gives an error about it being in landscape mode. How do I fix this to say it's okay to flip it to portrait. thanks

-(IBAction)takePhoto{
takePicker = [[UIImagePickerController alloc]init];
takePicker.delegate = self;
[takePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:takePicker animated:YES completion:NULL];

}
-(IBAction)chooseExisiting{
choosePicker = [[UIImagePickerController alloc]init];
choosePicker.delegate = self;
[choosePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:choosePicker animated:YES completion:NULL];

}

Solution

  • You don't have to use ImagePickerController in portrait mode. Just subclass it to open in landscape mode :

    @interface NonRotatingUIImagePickerController : UIImagePickerController
    @end
    
    @implementation NonRotatingUIImagePickerController
    - (BOOL)shouldAutorotate
    {
        return NO;
    }
    @end
    

    After that, you can use your code with that class.

    -(IBAction)takePhoto {
    takePicker = [[NonRotatingUIImagePickerController alloc]init];
    takePicker.delegate = self;
    [takePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:takePicker animated:YES completion:NULL];
    }
    
    -(IBAction)chooseExisiting{
    choosePicker = [[NonRotatingUIImagePickerController alloc]init];
    choosePicker.delegate = self;
    [choosePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:choosePicker animated:YES completion:NULL];
    }