Unable to record dual photo from dual camera on iPhone X

I am trying to capture both telephoto and wide-angle cameras on the iPhoneX at the same time. This is how I initialized the device:

let captureDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back)

and I requested the delivery of two photos for AVPhotoOutput:

let photoSettings = AVCapturePhotoSettings()

photoSettings.isDualCameraDualPhotoDeliveryEnabled = true

capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)

However, I ran into this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCapturePhotoOutput setDualCameraDualPhotoDeliveryEnabled:] Dual Camera dual photo delivery is not supported in this configuration'

Are there any additional settings that need to be turned on or off?

+4
source share
1 answer

You must make sure that your capture device, capture session and capture output are configured correctly:

  1. Get the capture device using the following parameters (which are already correct in your code): AVCaptureDeviceTypeBuiltInDualCamera, AVMediaTypeVideo, AVCaptureDevicePositionBack

  2. AVCaptureDeviceInput, , 1.

  3. AVCaptureSession sessionPreset AVCaptureSessionPresetPhoto
  4. AVCapturePhotoOutput
  5. AVCaptureDeviceInput AVCapturePhotoOutput AVCaptureSession
  6. DualCameraDualPhotoDeliveryEnabled AVCapturePhotoOutput YES

(Objective-C):

// Create capture device discovery session to retrieve devices matching our
// needs
// -------------------------------------------------------------------------------
AVCaptureDeviceDiscoverySession*    captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInDualCamera]
                                                                                                                           mediaType:AVMediaTypeVideo
                                                                                                                            position:AVCaptureDevicePositionBack];

// Loop through the retrieved devices and select correct one
// -------------------------------------------------------------------------------
for(AVCaptureDevice* device in [captureDeviceDiscoverySession devices])
{
    if(device.position == AVCaptureDevicePositionBack)
    {
        self.captureDevice = device;
        break;
    }
}

// Get camera input
// -------------------------------------------------------------------------------
NSError*                error               = nil;
AVCaptureDeviceInput*   videoDeviceInput    = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];

if(!videoDeviceInput)
{
    NSLog(@"Could not retrieve camera input, error: %@", error);
    return;
}

// Initialize capture session
// -------------------------------------------------------------------------------   
self.captureSession                 = [[AVCaptureSession alloc] init];
self.captureSession.sessionPreset   = AVCaptureSessionPresetPhoto;

// Add video device input and photo data output to our capture session
// -------------------------------------------------------------------------------
self.captureOutput  = [AVCapturePhotoOutput new];
[self.captureSession beginConfiguration];
if(![self.captureSession canAddOutput:self.captureOutput])
{
    NSLog(@"Cannot add photo output!");
    return;
}

[self.captureSession addInput:videoDeviceInput];
[self.captureSession addOutput:self.captureOutput];
[self.captureSession commitConfiguration];

// Configure output settings AFTER input & output have been added to the session
// -------------------------------------------------------------------------------
if(self.captureOutput.isDualCameraDualPhotoDeliverySupported)
    self.captureOutput.dualCameraDualPhotoDeliveryEnabled = YES;

// Create video preview layer for this session, and add it to our preview UIView
// -------------------------------------------------------------------------------
AVCaptureVideoPreviewLayer* videoPreviewLayer   = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
videoPreviewLayer.videoGravity                  = AVLayerVideoGravityResizeAspect;
videoPreviewLayer.frame                         = self.view.bounds;
[self.view.layer addSublayer:videoPreviewLayer];

// Start capturing session
// -------------------------------------------------------------------------------
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    [self.captureSession startRunning];
});
  1. , AVCapturePhotoOutput, AVCapturePhotoSettings dualCameraDualPhotoDeliveryEnabled, YES.

(Objective-C):

AVCapturePhotoSettings* settings = [AVCapturePhotoSettings photoSettingsWithFormat:@{AVVideoCodecKey: AVVideoCodecTypeJPEG}];
settings.dualCameraDualPhotoDeliveryEnabled = YES;
[self.captureOutput capturePhotoWithSettings:settings delegate:self];
0

Source: https://habr.com/ru/post/1689135/


All Articles