0

我有一个 vc,它以模态方式添加一个图像选择器控制器,然后在选择 pic 时返回到同一个 vc。通过 PickerController 获取图像后,我将图像添加到屏幕上,如果图片是横向的,我希望设备自动更改为横向,如果图片是纵向,我希望设备自动更改为纵向。

我没有成功尝试这两种方法来强制设备进入横向

1. 

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];

2. 
[UIViewController attemptRotationToDeviceOrientation]; 

-(NSUInteger)supportedInterfaceOrientations
{

if (!isPortrait) //isPortrait is false when pic is landscape
    return UIInterfaceOrientationMaskLandscape;

}

或者,我想知道是否最好只为图片实例化一个单独的 vc。在这个问题之后我已经避免了它

4

1 回答 1

0

我的猜测是你必须在 ViewController[[UIApplication sharedApplication] setStatusBarOrientation:的方法内部调用.viewWillAppear:UIImagePickerController

为此,您需要首先了解图像方向,您可以执行以下操作:

- (BOOL)shouldAutorotate {
    return NO;
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if(myOrientation>0)
        [UIApplication sharedApplication].statusBarOrientation = myOrientation;
}

- (void)presentPicker {
    UIImagePickerController* imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.delegate = self;
    [self presentViewController:imgPicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    if(image.size.width>image.size.height)
        myOrientation = UIInterfaceOrientationLandscapeLeft;
    else
        myOrientation = UIInterfaceOrientationPortrait;

    [picker dismissViewControllerAnimated:YES completion:nil];
}

另外,不要忘记在 plist 中设置初始和支持的界面方向(第一个支持的方向必须等于初始方向)

于 2013-04-20T09:47:44.337 回答