Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVCam Project Crashes On iPad

Apple's demo source code for the AVCam demo app found here: https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html crashes when attempting to take a picture (regardless of whether you build the Objective-C or Swift versions) on the line in the AVCamCameraViewController/CameraViewController(Swift) that captures the photo:

[self.photoOutput capturePhotoWithSettings:photoSettings delegate:photoCaptureDelegate];

or (Swift)

self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureDelegate)

The error message when it crashes is:

2016-11-21 17:44:31.590070 AVCam[2178:2303627] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:] flashMode must be set to a value present in the supportedFlashModes array'

And when I examine the flash modes array, I get this:

(lldb) po [self.photoOutput supportedFlashModes] <__NSSingleObjectArrayI 0x170007c50>( 0 )

So in order to add the flash mode, the docs say you have to specify what modes you want to support in the AVCapturePhotoSettings object. I've done that with this line of code:

photoSettings.flashMode = AVCaptureFlashModeAuto;

or (Swift)

photoSettings.flashMode = .auto

So my hunch is that this is a bug specifically related to the 12.9" iPad Pro and I probably need to submit a radar, but thought I would ask here in case someone's seen it before. Any ideas?

Update

I've been able to duplicate this other iPads as well, so it doesn't appear to be only the 12.9" iPad Pro only.

like image 222
Matt Long Avatar asked Nov 22 '16 01:11

Matt Long


Video Answer


2 Answers

In swift it has to be checked whether photo output supports flash mode at all.

 let settings = AVCapturePhotoSettings()
 if photoOutput.supportedFlashModes.contains(.on) {
     settings.flashMode = self.flashMode
 } else {
     settings.flashMode = .off
 }

 self.photoOutput?.capturePhoto(with: settings, delegate: self)
like image 161
JPetric Avatar answered Sep 21 '22 23:09

JPetric


Add this ...

let position = self.videoDeviceInput.device.position
photoSettings.flashMode = position == .front || position == .unspecified ? .off : .auto

right before this ...

self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureDelegate)

That should fix the problem.

like image 32
Soja Avatar answered Sep 18 '22 23:09

Soja