0

我有一個模塊化添加圖像選擇器控制器的vc,然後在選擇圖片時返回相同的vc。在通過PickerController獲取圖像後,我將圖像添加到屏幕上,並且如果圖像是風景,我希望設備自動切換到風景,如果圖像是肖像,則需要人像。無法強制設備更改UIOrientation

我已經嘗試過失敗這兩種方法來強制設備進入景觀

1. 

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft]; 

2. 
[UIViewController attemptRotationToDeviceOrientation]; 

-(NSUInteger)supportedInterfaceOrientations 
{ 

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

} 

另外,我想知道如果這是最好的只是實例化圖像的單獨的VC。我避開它迄今在此之後question

回答

0

我的猜測是,你必須調用[[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中(第一支持的方向必須等於初始方向)