5

在我的 IOS 应用程序中,当我打开相机时,我在相机视图上放置了一张图像。在纵向模式下看起来不错。但是当它更改为横向模式时,它看起来有些奇怪。所以我想锁定UIImagePickerController纵向模式。

以下是我的 ui 图像选择器控制器代码

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

如何将其锁定为纵向模式....

4

8 回答 8

3

或者,您可以继承 UIImagePickerController:

创建一个新的 UIImagePickerController 类并将这些行添加到它。

- (BOOL)shouldAutorotate{
return NO;
}

将其导入到使用相机的类中,而不是使用默认的 UIImagePickerController,而是使用您创建的类。

您的相机本身应该停止自动旋转。

于 2013-11-26T03:35:55.220 回答
2

这不是最好的解决方案,但它有效:

AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:cameraOverlay];
imgPicker.cameraOverlayView = appDelegate.window.superview;

背景上的相机仍在旋转,但您的叠加视图不会。希望对你有帮助

于 2012-04-23T14:28:58.543 回答
2

唯一对我有用的解决方案是类别,但我也必须添加另一种方法:

#import "UIImagePickerController+NoRotate.h"

@implementation UIImagePickerController (NoRotate)

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

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

干杯!

于 2013-03-21T19:41:24.773 回答
1

您不必“将 UIImagePicker 控制器锁定为纵向模式”。正如您所说的“当它更改为横向模式时,它看起来有些奇怪”实际上我不知道您为什么说它看起来很奇怪。但是,这是我的 UIImagePicker 视图在横向模式下看起来很奇怪的体验。即:当 AViewController 作为根视图控制器时。并且 BViewController 的视图将子视图添加到 AViewController 的视图中。在BViewController presentModalViewController:UIImagePickerController中。UIImagePicker 视图在横向模式下看起来很奇怪。

这个问题的解决方案是UIImagePickerController在presentModelViewController之前设置为根视图控制器。下面的源代码显示了详细信息:

- (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];
    ....
}

我希望这个解决方案可以在未来帮助其他人。——戈曼

于 2012-07-11T07:50:57.393 回答
0

只需在您的视图控制器中编写此代码

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
于 2012-01-20T11:22:12.633 回答
0

添加一个类别UIImagePickerController并覆盖它的shouldAutoRotateToInterfaceOrientation方法,如下:

@implementation UIImagePickerController (NoRotate)

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

@end
于 2012-01-20T11:59:02.960 回答
0
-(BOOL)shouldAutorotate {
return NO;}

在您的视图控制器中尝试此操作。这对我有用。注意:这是针对ios6.0及以上的

于 2013-10-09T08:52:38.700 回答
0

无需创建子类,只需为 uiimagepickercontroller 创建一个类别并将这一行放在上面 - (BOOL)shouldAutorotate{ return NO; }

于 2014-07-31T14:41:30.843 回答