lock UIImagePickerController in Portrait mode in i

2020-07-13 11:46发布

In my IOS app, when I open the camera I have placed an image over the camera view. It looks good in portrait mode. But when it is changed to landscape mode it looks some odd. So I want to lock the UIImagePickerController in Portrait mode.

Following is my code for ui image picker controller

UIImagePickerController *imgPkr = [[UIImagePickerController alloc] init];
imgPkr.delegate = self;
imgPkr.sourceType = UIImagePickerControllerSourceTypeCamera;

How it can be locked in portrait mode....

8条回答
时光不老,我们不散
2楼-- · 2020-07-13 11:52

Just write this code in your view controller

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
查看更多
贪生不怕死
3楼-- · 2020-07-13 11:53
-(BOOL)shouldAutorotate {
return NO;}

Try this in your view controller. This worked for me. Note: This is for ios6.0 and above

查看更多
Bombasti
4楼-- · 2020-07-13 11:57

Add a category on UIImagePickerController and override it's shouldAutoRotateToInterfaceOrientation method, as follows:

@implementation UIImagePickerController (NoRotate)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
查看更多
欢心
5楼-- · 2020-07-13 11:59

The only solution that worked for me was the category, but I had to add another method too:

#import "UIImagePickerController+NoRotate.h"

@implementation UIImagePickerController (NoRotate)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
@end

Cheers!

查看更多
别忘想泡老子
6楼-- · 2020-07-13 12:04

You don't have to "lock the UIImagePicker Controller in Portrait mode". As you said "when it is changed to landscape mode it looks some odd" Actually I don't know why you say it look odd. But, here is my experience of UIImagePicker view look odd in landscape mode. That is: When AViewController as the root view controller. And BViewController's view add subview to AViewController's view. And presentModalViewController:UIImagePickerController in BViewController. The UIImagePicker view will look odd in landscape mode.

The solution to this problem is set the UIImagePickerController as the root view controller before presentModelViewController. The source code below show the detail:

- (void) onCameraButtonTapped:(UIBarButtonItem *)buttonItem
{   
    //backupRootController it's use as a backup, it will recover after close the image picker controller.
    self.backupRootController = [[UIApplication sharedApplication] keyWindow].rootViewController;

    UIImagePickerController * imageController = [[UIImagePickerController alloc] init];
    imageController.sourceType = UIImagePickerControllerSourceTypeCamera;
    imageController.delegate = self;
    ....
    [[[UIApplication sharedApplication] keyWindow] setRootViewController:imageController];
    [self presentModalViewController:imageController animated:YES];
    [imageController release];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [[[UIApplication sharedApplication] keyWindow]  setRootViewController:self.backupRootController];
    ....
}

I hope this solution can help other person in the future. --Goman

查看更多
看我几分像从前
7楼-- · 2020-07-13 12:05

there is no need to create a subclass, just create a category for uiimagepickercontroller and put this line on it - (BOOL)shouldAutorotate{ return NO; }

查看更多
登录 后发表回答