我正在尝试为vr应用并排实现两个摄像头视图。我在这个网站上找到了一些有用的信息:
How to show 2 camera preview side by side?[For cardboard apps]
但我想如何使用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];
}