UIImagePickerController in Landscape

社会主义新天地 提交于 2019-11-26 17:28:58

I haven't checked whether this is illegal, but it worked for me. If you want the UIImagePickerController to start(and stay) in Landscape orientation code:

//Initialize picker

UIImagePickerController * picker = [[UIImagePickerController alloc] init];
   picker.delegate = self;


//set Device to Landscape. This will give you a warning. I ignored it.
//warning: 'UIDevice' may not respond to '-setOrientation:'


[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//Set Notifications so that when user rotates phone, the orientation is reset to landscape.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

//Refer to the method didRotate:   
[[NSNotificationCenter defaultCenter] addObserver:self
              selector:@selector(didRotate:)
               name:@"UIDeviceOrientationDidChangeNotification" object:nil];

//Set the picker source as the camera   
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

//Bring in the picker view   
[self presentModalViewController:picker animated:YES];

The method didRotate:

- (void) didRotate:(NSNotification *)notification

{
      //Maintain the camera in Landscape orientation
 [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

No need to subclass; simply override the modalPresentationStyle property.

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.modalPresentationStyle = UIModalPresentationFormSheet;
    [viewController presentViewController:picker animated:YES completion:NULL];

If you just need to get rid of the warning try

@interface UIDevice ()
    -(void)setOrientation:(UIDeviceOrientation)orientation;
@end
imaginaryunit

I've developed a UIImagePicker class in landscape mode. Works great for applications I've developed: hope it works for you too:

GitHub: https://github.com/imaginaryunit/iOSLandscapeThreadedImagePicker.git

I have an all-landscape app using UIImagePickerController too. Please be noted that if you call UIImagePickerController in Landscape mode, your app is possible to be rejected by Apple Review Team.

I devised a simple work around this issue which make use the shouldAutoRotate delegate. Apple approves this method for an all-landscape app.

See here for the details and downloadable full project source code.

Make a category of UINavigationController and add this method

- (BOOL)shouldAutorotate
{
    return NO;
}

Subclass UIImagePickerController and override modalPresentationStyle as follows:

- (UIModalPresentationStyle)modalPresentationStyle
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        return UIModalPresentationFormSheet;
    }

    return [super modalPresentationStyle];
}

The image-picker is a form-sheet now and no longer in fullscreen-mode, but it looks good in landscape-mode. This should be totally app-store-safe.

This works for the gallery, not for taking pictures.

I solved this problem as follows: after each change in orientation, I simple re-create picker. Try this. Differently is too crooked...

I am developing a class that tries its best to work in landscape mode. Check it out on GitHub: RACameraController

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!