Ios 相机只能在纵向模式下工作

Ios 相机只能在纵向模式下工作,ios,swift,camera,landscape,portrait,Ios,Swift,Camera,Landscape,Portrait,我的相机在纵向模式下看起来像这样: 就像在横向模式下一样: 因此,我的问题是: 如何在横向模式下全屏设置预览层 当我旋转设备时,为什么相机不旋转?它始终处于纵向模式 确保手机的“纵向锁定”功能已关闭。并确保应用程序在“项目设置”>“常规”选项卡中允许横向定位 确保手机的“纵向锁定”功能已关闭。并确保应用程序在“项目设置”>“常规”选项卡中允许横向定位 每次更改设备方向时,都应设置AVCaptureSession方向 添加通知观察员以观察设备方向的任何变化,然后根据设备方向设置AVCaptur

我的相机在纵向模式下看起来像这样:

就像在横向模式下一样:

因此,我的问题是:

  • 如何在横向模式下全屏设置预览层
  • 当我旋转设备时,为什么相机不旋转?它始终处于纵向模式

  • 确保手机的“纵向锁定”功能已关闭。并确保应用程序在“项目设置”>“常规”选项卡中允许横向定位

    确保手机的“纵向锁定”功能已关闭。并确保应用程序在“项目设置”>“常规”选项卡中允许横向定位

    每次更改设备方向时,都应设置
    AVCaptureSession
    方向

    添加通知观察员以观察设备方向的任何变化,然后根据设备方向设置
    AVCaptureSession
    方向

    Obj-C:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    -(void) orientationChanged
    {
        if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
    
        if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
    
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    }
    
    Swift 2:

    在某处添加观察者,例如在
    viewDidLoad
    方法中

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(orientationChanged), name: UIDeviceOrientationDidChangeNotification, object: nil)
    
    然后:

    您也可以提供您的任何代码,或者查看这个问题,它可能会有所帮助。

    每次更改设备方向时,您都应该设置
    AVCaptureSession
    方向

    添加通知观察员以观察设备方向的任何变化,然后根据设备方向设置
    AVCaptureSession
    方向

    Obj-C:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    -(void) orientationChanged
    {
        if (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
    
        if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
    
        if (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    }
    
    Swift 2:

    在某处添加观察者,例如在
    viewDidLoad
    方法中

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(orientationChanged), name: UIDeviceOrientationDidChangeNotification, object: nil)
    
    然后:

    您也可以提供您的任何代码,或者查看这个问题,它可能会有所帮助。

    也许你可以编辑你的评论,使其在swift中有用?也许你可以编辑你的评论,使其在swift中有用?