Вы должны установить ориентацию 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
Возможно, вы могли бы отредактировать свой комментарий, чтобы он был полезен в быстром? – mafioso
Хорошо, я обновлю код Swift. –