多个AVCaptureVideoDataOutput在同一个AVCaptureSession中

我想知道是否有可能添加多个AVCaptureVideoDataOutputAVCaptureSession与一个摄像头设备的input?

我的实验指数添加第二个VideoDataOutput将导致canAddOutput返回NO。 但是我找不到苹果的文档上的任何地方说多个数据输出是不允许的。

我们不能将单个AVCaptureSession用于多个AVCaptureVideoDataOutput对象。

你能做什么是你可以使用多个AVCaptureSession对象制作多个AVCaptureVideoDataOutput

您可以创build两个不同的AVCaptureVideoDataOutputAVCaptureSession设置,然后您可以在应用程序中一个接一个地使用它们,并且您将能够实现目标。

在我的情况下,我必须一次使用相机捕捉正面和背面的图像。

我确实为AVCaptureVideoDataOutputAVCaptureSession创build了两个不同的对象,如下所示。

 /* Front camera settings */ @property bool isFrontRecording; @property (strong, nonatomic) AVCaptureDeviceInput *videoInputBack; @property (strong, nonatomic) AVCaptureStillImageOutput *imageOutputBack; @property (strong, nonatomic) AVCaptureSession *sessionBack; /* Back camera settings */ @property bool isBackRecording; @property (strong, nonatomic) AVCaptureDeviceInput *videoInputFront; @property (strong, nonatomic) AVCaptureStillImageOutput *imageOutputFront; @property (strong, nonatomic) AVCaptureSession *sessionFront; 

现在看来确实加载最初我没有安装回相机,并为会议开始录制设置标志或不为两个会议如下。

 - (void)viewDidLoad { [super viewDidLoad]; [self setupBackAVCapture]; self.isFrontRecording = NO; self.isBackRecording = NO; } - (void)setupBackAVCapture { NSError *error = nil; self.sessionBack = [[AVCaptureSession alloc] init]; self.sessionBack.sessionPreset = AVCaptureSessionPresetPhoto; AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.videoInputBack = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error]; [self.sessionBack addInput:self.videoInputBack]; self.imageOutputBack = [[AVCaptureStillImageOutput alloc] init]; [self.sessionBack addOutput:self.imageOutputBack]; } 

现在,每当用户开始拍摄照片,我们将使用下面的代码捕捉前面的照片。

 - (IBAction)buttonCapture:(id)sender { [self takeBackPhoto]; } - (void)takeBackPhoto { [self.sessionBack startRunning]; if (!self.isFrontRecording) { self.isFrontRecording = YES; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); AVCaptureConnection *videoConnection = [self.imageOutputBack connectionWithMediaType:AVMediaTypeVideo]; if (videoConnection == nil) { return; } [self.imageOutputBack captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (imageDataSampleBuffer == NULL) { return; } NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData]; UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); [self.imageView setImage:image]; [self.sessionBack stopRunning]; // Set up front camera setting and capture photo. [self setupFrontAVCapture]; [self takeFrontPhoto]; }]; self.isFrontRecording = NO; } } 

一旦后面的图像被捕获,我们将设置会话以使用setupFrontAVCapture方法捕获前方图像,然后我们将使用下面给出的takeFrontPhoto方法捕获前方图像。

 - (void)setupFrontAVCapture { NSError *error = nil; self.sessionFront = [[AVCaptureSession alloc] init]; self.sessionFront.sessionPreset = AVCaptureSessionPresetPhoto; AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; camera = [self cameraWithPosition:AVCaptureDevicePositionFront]; self.videoInputFront = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error]; [self.sessionFront addInput:self.videoInputFront]; self.imageOutputFront = [[AVCaptureStillImageOutput alloc] init]; [self.sessionFront addOutput:self.imageOutputFront]; } - (void)takeFrontPhoto { [self.sessionFront startRunning]; if (!self.isBackRecording) { self.isBackRecording = YES; AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); AVCaptureConnection *videoConnection = [self.imageOutputFront connectionWithMediaType:AVMediaTypeVideo]; if (videoConnection == nil) { return; } [self.imageOutputFront captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) { if (imageDataSampleBuffer == NULL) { return; } NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData]; UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); [self.imageViewBack setImage:image]; [self.sessionFront stopRunning]; }]; self.isBackRecording = NO; } } 

这样你可以使用两套不同的AVCaptureSessionAVCaptureStillImageOutput对象,你可以实现你的目标。

请让我知道,如果你有任何困惑。