Как показать два предварительных просмотра камеры в iOS target-C для приложения vr?
Я пытаюсь реализовать бок о бок два вида камеры для приложения vr. Я нашел некоторую полезную информацию на этом сайте:
Как показать 2 камеры предварительного просмотра рядом?[Для картонных приложений]
но я хочу, как создать его с iOS?
Я пытаюсь создать такое же поведение с AVFoundation. Это мой текущий код
CameraViewController
#import <AVFoundation/AVFoundation.h>
#import "CameraViewController.h"
@interface CameraViewController ()
@property (strong, nonatomic) AVCaptureSession *captureSession;
@property (strong, nonatomic) AVCaptureStillImageOutput *stillImageOutput;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayerLeft;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayerRight;
@end
@implementation CameraViewController
#pragma mark - lazy instantiation
- (AVCaptureSession *)captureSession {
if (!_captureSession)
_captureSession = [[AVCaptureSession alloc] init];
return _captureSession;
}
- (AVCaptureStillImageOutput *)stillImageOutput {
if (!_stillImageOutput)
_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
return _stillImageOutput;
}
- (AVCaptureVideoPreviewLayer *)previewLayerLeft {
if (!_previewLayerLeft)
_previewLayerLeft = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
return _previewLayerLeft;
}
- (AVCaptureVideoPreviewLayer *)previewLayerRight {
if (!_previewLayerRight)
_previewLayerRight = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
return _previewLayerRight;
}
#pragma mark - view controller's life cycle methods
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:&error];
if ([self.captureSession canAddInput:deviceInput])
[self.captureSession addInput:deviceInput];
[self.previewLayerLeft setVideoGravity:AVLayerVideoGravityResizeAspect];
[self.previewLayerRight setVideoGravity:AVLayerVideoGravityResizeAspect];
CALayer *rootLayer = [self.view layer];
[rootLayer setMasksToBounds:YES];
NSLog(@"%@", NSStringFromCGRect([rootLayer frame]));
if ([self.previewLayerLeft.connection isVideoOrientationSupported])
[self.previewLayerLeft.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc
[self.previewLayerLeft setFrame:CGRectMake(0, 0, [rootLayer frame].size.width / 2, [rootLayer frame].size.height)];
if ([self.previewLayerRight.connection isVideoOrientationSupported])
[self.previewLayerRight.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight]; //home button on right. Refer to .h not doc
[self.previewLayerRight setFrame:CGRectMake([rootLayer frame].size.width / 2, 0, [rootLayer frame].size.width / 2, [rootLayer frame].size.height)];
[rootLayer insertSublayer:self.previewLayerLeft atIndex:0];
[rootLayer insertSublayer:self.previewLayerRight atIndex:1];
[self.stillImageOutput setOutputSettings:[[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil]];
if ([self.captureSession canAddOutput:self.stillImageOutput])
[self.captureSession addOutput:self.stillImageOutput];
[self.captureSession startRunning];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}