I am trying to modify the exposure duration of photos taken.
Within the loop:
for (customDuration, customISO) in zip(exposureDurations, exposureISOs) { // exposureDurations and exposureISOs are just arrays of exposures
// code here
}
I set the AVCapture device's exposure to my custom value:
captureDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera,
for: AVMediaType.video,
position: .back) {
captureSession = AVCaptureSession()
capturePhotoOutput = AVCapturePhotoOutput()
do { try captureDevice.lockForConfiguration() }
catch { print("capturebracket: cannot lock camera for configuration."); return }
// set the correct exposure
captureDevice.setExposureModeCustom(duration: customDuration, iso: customISO, completionHandler: { (_) -> Void in exposureModeSet = true })
// unlock the capture device (sets changes)
captureDevice.unlockForConfiguration()
// wait for changes to propagate
while !exposureModeSet {}
// get the correct photo settings
let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg, AVVideoCompressionPropertiesKey : [AVVideoQualityKey : jpegQuality]])
Then I print the value to make sure it's been set correctly:
print("duration: \(captureDevice.exposureDuration)") // prints customExposure
print("device duration: \(( captureSession.inputs[0] as? AVCaptureDeviceInput)!.device.exposureDuration)") // prints customExposure
print("device duration: \((capturePhotoOutput.connections[0].inputPorts[0].input as? AVCaptureDeviceInput)!.device.exposureDuration)") // prints customExposure
photoSettings.isAutoStillImageStabilizationEnabled = false
Then I take the photo:
capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)
Somehow, when I loop through the above code with different exposures, the photos end up getting the wrong exposures. Specifically, the first run through works fine. The second will sometimes take a photo with the first exposure, sometimes with the second. The third will sometimes take a photo with the second exposure, sometimes with the third, and so on.
The interesting thing is that when I insert a long, say 1-3 second pause just before taking the photo, all of the exposures are set correctly... so it seems that the changes to AVCaptureDevice are taking time to propagate, and the photoOutput rushes ahead and takes the photo before the exposure has been set correctly.
Does anyone have any ideas? Can't seem to find anything in the docs about it.