19

我正在創建一個處於橫向模式的應用程序,我正在使用UIImagePickerController拍攝使用iPhone相機的照片,我也想在橫向模式下創建它。橫向使用UIImagePickerController

但是,正如蘋果文檔建議UIImagePickerController不支持橫向導向,那麼我應該怎麼做才能獲得所需的功能?

+0

它似乎在iOS 8 beta 4中工作。唯一的問題是在拍攝照片後,它會以縱向模式向您顯示屏幕(使用此照片或取消)。但圖像在橫向上捕獲的很好。 –

回答

44

如果您想使用的UIImagePickerController在橫向模式下,使用user1673099's answer,但不是:

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

使用:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

,然後選擇器會在開闊的景觀模式:

enter image description here

但是讓s URE您在部署信息查詢肖像:

enter image description here

+0

它的效果非常好。一個問題,爲什麼我們應該在部署信息中檢查肖像?我知道這是必要的,但我不明白原因。 –

+2

@AnsonYao因爲'UIImagePickerController'只支持肖像模式。如果您不支持縱向「全局」,圖像選擇器將因爲沒有可用的方向而崩潰。 – pkamb

+0

@ Mc.Lover它的工作如果我檢查部署信息中的肖像模式。但它只是改變了我整個應用程序的方向。請你告訴我,我該如何解決這個問題 –

11

嘗試這樣....

按照蘋果的文檔,ImagePicker控制器從未在風景模式下轉動。您只能在肖像模式下使用。

對於禁用風景模式僅適用於ImagePicker控制器遵循下面的代碼:

在你ViewController.m:

將該子類圖像(NonRotatingUIImagePickerController)選取器控制器

@interface NonRotatingUIImagePickerController : UIImagePickerController 

@end 

@implementation NonRotatingUIImagePickerController 
// Disable Landscape mode. 
- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 
@end 

使用如下

UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init]; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     picker.delegate = self; 
    etc.... Just as Default ImagePicker Controller 

這對我有用&讓我知道你是否有任何問題。

+0

這對你有幫助嗎? – user1673099

+0

我不想禁用橫向模式,我只想在橫向模式下使用圖像選擇器控制器而不是在portraite模式下。 我想知道如果我可以旋轉圖像選擇器拍照以及不? –

+0

@ParasGorasiya,ImagePicker將永遠不會支持橫向模式。它只支持肖像模式。 – user1673099

5

正確的方式在橫向模式下使用UIImagePickerController沒有任何黑客是把它變成一個UIPopoverController

- (void)showPicker:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

    _popover = [[UIPopoverController alloc] initWithContentViewController:picker]; 
    [_popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 
+2

只適用於UIUserInterfaceIdiomPad –

+0

@ iTroyd23它可以通過添加類別到UIPopoverController黑客攻擊 – yershuachu

+1

@yershuachu對,所以你用另一個黑客替換? ;-) –

3

修改上面的代碼方法

- (NSUInteger)supportedInterfaceOrientations 
{ 
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
    if(orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft) 
     return UIInterfaceOrientationMaskLandscape; 
    else 
     return UIInterfaceOrientationMaskPortrait; 
} 
1

接受的答案對我無效。我還需要將modalPresentationStyle添加到UIImagePickerController以使其工作。

UIImagePickerController *pickerController = [[UIImagePickerController alloc] init]; 
pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; 
pickerController.delegate = self; 
pickerController.allowsEditing = YES; 
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
[self presentViewController:pickerController animated:YES completion:nil]; 

當然還有要記得把這個在您的委託:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskLandscape; 
} 

不過根據蘋果的文檔,這是不支持呈現這個選擇器在橫向模式,所以要小心了。

6

...我也想在橫向模式下創建它。

一行代碼可以有很大的區別!在方法或函數在您IBAction爲土地:

在斯威夫特,

let imagePickerController = UIImagePickerController() 
imagePickerController.delegate = self 
// .overCurrentContext allows for landscape and portrait mode 
imagePickerController.modalPresentationStyle = .overCurrentContext 

的Objective-C,

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
[imagePickerController setDelegate:self]; 
[imagePickerController setModalPresentationStyle: UIModalPresentationOverCurrentContext]; 

注:這將允許imagePickerController提出它的觀點正確,但 可能不能解決提交時的旋轉問題。

3

下面是支持所有的接口方向轉動一個版本:

/// Not fully supported by Apple, but works as of iOS 11. 
class RotatableUIImagePickerController: UIImagePickerController { 

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
    return .all 
    } 
} 

若是使用者轉動她的設備通過這種方式,它會更新選擇器控制器,能支持目前的方向。只是像通常那樣實例化一個UIImagePickerController。

如果您只想支持一個方向子集,您可以返回一個不同的值。

1

這與在iOS 10/11中的Swift 4.0很適合。

import UIKit 

extension UIImagePickerController { 
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
     return .all 
    } 
} 

只需將擴展程序放在項目的某個位置,不需要子類化它的任何工作。

如果需要指定設備類型,可以添加一個檢查這樣的:

import UIKit 

extension UIImagePickerController { 
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
     return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all 
    } 
} 

這將使iPad的自由旋轉,但在手機上強制肖像模式。只要確保您的應用程序已配置爲在其info.plist中支持這些應用程序,否則在啓動選取器時可能會遇到崩潰。

相關問題