Video Guardando en la orientación incorrecta AVCaptureSession

Estoy intentando grabar un video (sin mostrar la camera) y savelo. Pero el video que se está guardando no está guardando en la orientación correcta. He tratado de forzar al UIViewController para que sea una orientación determinada, pero eso no ayudó. Todos los videos se graban en retrato. Mi código está debajo:

session = [[AVCaptureSession alloc] init]; [session beginConfiguration]; session.sessionPreset = AVCaptureSessionPresetHigh; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *cam in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) { if (cam.position == AVCaptureDevicePositionFront) device = cam; } NSError *error = nil; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (!input) { // Handle the error appropriately. NSLog(@"ERROR: trying to open camera: %@", error); } AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; NSString *outputpathofmovie = [[documentsDirectoryPath stringByAppendingPathComponent:@"RecordedVideo"] stringByAppendingString:@".mp4"]; outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie]; [self deleteTempVideos]; [session addInput:input]; [session addInput:audioInput]; [session commitConfiguration]; [session startRunning]; movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; NSLog(@"%@", movieFileOutput.connections); AVCaptureConnection *videoConnection = nil; for ( AVCaptureConnection *connection in [movieFileOutput connections] ) { NSLog(@"%@", connection); for ( AVCaptureInputPort *port in [connection inputPorts] ) { NSLog(@"%@", port); if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) { videoConnection = connection; } } } if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false** { [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]]; } NSLog(@"%@", videoConnection); [session addOutput:movieFileOutput]; [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 

Resulta que debe agregar la orientación de las conexiones a AVCaptureMovieFileOutput después de que se agregue a la session.

 session = [[AVCaptureSession alloc] init]; [session beginConfiguration]; session.sessionPreset = AVCaptureSessionPresetHigh; AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *cam in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) { if (cam.position == AVCaptureDevicePositionFront) device = cam; } NSError *error = nil; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (!input) { // Handle the error appropriately. NSLog(@"ERROR: trying to open camera: %@", error); } AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectoryPath = [paths objectAtIndex:0]; NSString *outputpathofmovie = [[documentsDirectoryPath stringByAppendingPathComponent:@"RecordedVideo"] stringByAppendingString:@".mp4"]; outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie]; [self deleteTempVideos]; [session addInput:input]; [session addInput:audioInput]; [session commitConfiguration]; [session startRunning]; movieFileOutput = [[AVCaptureMovieFileOutput alloc] init]; [session addOutput:movieFileOutput]; AVCaptureConnection *videoConnection = nil; for ( AVCaptureConnection *connection in [movieFileOutput connections] ) { NSLog(@"%@", connection); for ( AVCaptureInputPort *port in [connection inputPorts] ) { NSLog(@"%@", port); if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) { videoConnection = connection; } } } if([videoConnection isVideoOrientationSupported]) // **Here it is, its always false** { [videoConnection setVideoOrientation:[[UIDevice currentDevice] orientation]]; } [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self]; 

**

Intente cambiar su orientación justo antes de comenzar a grabar.

**

  if let videoConnection = fileOutput!.connectionWithMediaType(AVMediaTypeVideo) { var newOrientation: AVCaptureVideoOrientation? switch (UIDevice.currentDevice().orientation) { case .Portrait: newOrientation = .Portrait break case .PortraitUpsideDown: newOrientation = .PortraitUpsideDown break case .LandscapeLeft: newOrientation = .LandscapeRight break case .LandscapeRight: newOrientation = .LandscapeLeft break default : newOrientation = .Portrait break } self.fileOutput.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = newOrientation! self.fileOutput.startRecordingToOutputFileURL(NSURL(fileURLWithPath: filePath!), recordingDelegate: self) } 

donde ,

 var fileOutput : AVCaptureMovieFileOutput! 

Prueba algo como esto:

 #import <AVFoundation/AVFoundation.h> AVCaptureConnection *captureConnection = <#A capture connection#>; if ([captureConnection isVideoOrientationSupported]) { AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeLeft; [captureConnection setVideoOrientation:orientation]; } 

http://developer.apple.com/library/ios/#qa/qa1744/_index.html#//apple_ref/doc/uid/DTS40011134

Gracias Destiny Dawn, en caso de que alguien esté buscando el código para Xamarin ios:

 AVCaptureConnection videoConnection = null; foreach(AVCaptureConnection connection in _videoOutput.Connections) { foreach ( AVCaptureInputPort port in connection.InputPorts) { if (port.MediaType == AVMediaType.Video) { videoConnection = connection; break; } } } if (videoConnection != null) { if (videoConnection.SupportsVideoOrientation) { videoConnection.VideoOrientation = AVCaptureVideoOrientation.LandscapeRight; } }