我有一个带有 AVPreviewLayer 的全屏 avcapturesession。
当我切换相机时,预览层会正确显示视频,但一旦保存,视频就会颠倒。
我玩过镜像和横向左/右属性来连接视频输出文件,但无济于事。
这是切换相机的方法:
- (void)swapFrontAndBackCameras {
// Assume the session is already running
NSArray * inputs = self . session.inputs;
for (AVCaptureDeviceInput * INPUT in inputs) {
AVCaptureDevice * Device = INPUT.device ;
if ( [ Device hasMediaType : AVMediaTypeVideo ] ) {
AVCaptureDevicePosition position = Device.position;
AVCaptureDevice * newCamera = nil;
AVCaptureDeviceInput * newInput = nil;
if(position == AVCaptureDevicePositionFront) {
newCamera = [ self cameraWithPosition : AVCaptureDevicePositionBack];
}
else
{
newCamera = [ self cameraWithPosition : AVCaptureDevicePositionFront];
}
newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
[self.session beginConfiguration ] ;
[self.session removeInput:INPUT ] ;
[self.session addInput:newInput ] ;
AVCaptureConnection *videoConnection = nil;
for ( AVCaptureConnection *connection in [self.movieFileOutput connections] )
{
NSLog(@"%@", connection);
for ( AVCaptureInputPort *port in [connection inputPorts] )
{
NSLog(@"%@", port);
if ( [[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
}
}
}
[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; // This does it for the previewlayer only, not the output file :(
[self.session commitConfiguration ] ;
break;
}
}
}