AVCapturesession在录制时改变视频方向

时间:2016-08-17 05:45:44

标签: ios objective-c orientation avcapturesession

我需要在使用AVCaptureSession录制视频时更改视频方向。我在开始录制之前使用以下代码更改了方向,但我需要在录制过程中更改方向。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [self orientationChanged];
}

//Respond with the rotation 
- (void)orientationChanged {
    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;
                break;
            }
        }
    }

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (deviceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortraitUpsideDown];
    }
    else if (deviceOrientation == UIInterfaceOrientationPortrait) {
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    }

    else if (deviceOrientation == UIInterfaceOrientationLandscapeLeft) {
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
    }

    else {
        [_previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
        [videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用此代码在记录过程中更改方向:

AVCaptureConnection *videoConnection = [self.videoOutPut connectionWithMediaType:AVMediaTypeVideo];
- (void)_setOrientationForConnection:(AVCaptureConnection *)connection
{
    if (!connection || ![connection isVideoOrientationSupported])
        return;

    AVCaptureVideoOrientation captureOrientation = AVCaptureVideoOrientationPortrait;

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (deviceOrientation) {
        case UIDeviceOrientationLandscapeLeft:
            captureOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIDeviceOrientationLandscapeRight:
            captureOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIDeviceOrientationPortrait:
            captureOrientation = AVCaptureVideoOrientationPortrait;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            captureOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        default:
            break;
    }

    [connection setVideoOrientation:captureOrientation];
}