如何以横向模式启动相机

时间:2015-10-28 17:47:28

标签: ios objective-c avfoundation video-capture landscape-portrait

我强迫我的应用程序仅在横向模式而不是纵向上启动..在屏幕上我打开凸轮以使用AVFoundation在UIView中录制视频。但它的方向并没有像景观那样改变。想法?

- (void) CameraSetOutputProperties
{
//SET THE CONNECTION PROPERTIES (output properties)
AVCaptureConnection *CaptureConnection = [MovieFileOutput connectionWithMediaType:AVMediaTypeVideo];

//Set landscape (if required)
if ([CaptureConnection isVideoOrientationSupported])
{
    AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationLandscapeRight; //<<<<<SET VIDEO ORIENTATION IF LANDSCAPE
    [CaptureConnection setVideoOrientation:orientation];
}
}

//

- (void)initializeCamera {
captureSession = [[AVCaptureSession alloc]init];
captureSession.sessionPreset = AVCaptureSessionPresetHigh;

_previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
[_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  ....

if (!frontCamera) {
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];
    if (!input) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [captureSession addInput:input];
}
if (frontCamera) {
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:frontCamera error:&error];
    if (!input) {
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [captureSession addInput:input];
}

[captureSession startRunning];

1 个答案:

答案 0 :(得分:0)

如果您的方向发生变化但视频的方向仍然相同,那么您还必须更改AVCaptureConnection的videoOrientation属性,

你可以使用 viewWillTransitionToSize:withTransitionCoordinator:方法,

 override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
         super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        if size.width > size.height {
            capturePreviewLayer.connection.videoOrientation = .LandscapeLeft
        } else {
            capturePreviewLayer.connection.videoOrientation = .Portrait
        }
    }

这应该适合你的情况。