相机仅在纵向模式下工作

时间:2016-08-01 14:26:09

标签: ios swift camera landscape portrait

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

enter image description here

和横向模式一样:

enter image description here

所以我的问题是:

  1. 如何在横向模式下全屏设置previewLayer?
  2. 当我旋转设备时,为什么相机不会旋转?它始终处于纵向模式

2 个答案:

答案 0 :(得分:2)

每次更改设备的方向时,都应设置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];
}

斯威夫特2:

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

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

然后:

func orientationChanged(){
    let deviceOrientation = UIDevice.currentDevice().orientation

    if deviceOrientation == UIDeviceOrientation.PortraitUpsideDown {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
    }
    else if deviceOrientation == UIDeviceOrientation.Portrait {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
    }
    else if deviceOrientation == UIDeviceOrientation.LandscapeLeft {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
    }
    else if deviceOrientation == UIDeviceOrientation.LandscapeRight {
        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
    }
}

您也可以提供任何代码,或者在问题可能有所帮助之前查看此问题。 AVCaptureSession with multiple orientations issue

答案 1 :(得分:0)

确保手机的纵向锁定"被关闭了。并确保应用程序允许项目设置中的横向方向>常规选项卡。