I have a full screen avcapturesession with an AVPreviewLayer.
When I toggle the camera the previewlayer shows the video correctly, but once saved the video is upside down.
I have played around with mirroring and landscape left/right properties for the connection of the video output file, but to no avail.
This is the method that toggles the camera:
- (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;
}
}
}