答案 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)
确保手机的纵向锁定"被关闭了。并确保应用程序允许项目设置中的横向方向>常规选项卡。