2016-08-01 3 views
0

Моя камера выглядит в портретном режиме:Камера просто работает в портретном режиме

enter image description here

и т.п., что в ландшафтном режиме:

enter image description here

Так что мои вопросы:

  1. Как установить предварительный просмотр в полноэкранном режиме в LAN Режим dscape?
  2. Почему камера не вращается, когда я поворачиваю устройство? Он всегда остается в портретном режиме

ответ

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]; 
} 

Swift 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

+0

Возможно, вы могли бы отредактировать свой комментарий, чтобы он был полезен в быстром? – mafioso

+0

Хорошо, я обновлю код Swift. –

0

Убедитесь, что «блокировка портретной ориентации» телефона отключена. И убедитесь, что приложение позволяет использовать ландшафтные ориентации в настройках проекта> вкладка «Общие».

Смежные вопросы