Build an iPhone Sensing App Part 3: Camera Settings and a White Balance Bug
By 2025 the app had two capture paths, a working depth pipeline, and enough testers that changing a setting meant shipping a build. Field use kept calling for a different interval, a different lens, or the other camera type.
I added a settings sheet inside the app, and while building it I introduced a white balance bug.
What the screen needed to do
Three settings used to require a code change and a rebuild:
- Camera type: ARKit or AVFoundation, chosen per session.
- Capture timing: the interval for continuous capture.
- Lens selection: which physical camera AVFoundation should use.
The layout is simple because the phone is held at arm’s length over a plot. A segmented control at the top and a slider below it are about all that can be operated in that position.
Persisting the selection is plain UserDefaults, keyed by setting name, read once when the capture session is configured:
enum CameraSettingKey {
static let cameraType = "cameraType" // "ARKit" | "AVFoundation"
static let captureInterval = "captureInterval" // seconds
static let lens = "lensSelection" // AVCaptureDevice.DeviceType name
static let whiteBalance = "whiteBalanceMode"
}
Capture timing
The slider sets how often the app captures while the record button is held. At 0.10 s it behaves like a burst; at a second or more it is a sampling tool for walking a transect.
Two problems in this part of the rebuild showed up on screen.
The first is the black preview above. The timing control and the capture session were updated by separate code paths, so the label could show a new interval while the session was still stopped. Nothing was captured, and the app gave no warning.
The second is the layout. In the screenshot below the preview area is empty except for the bottom half, the ARKit switch is squeezed next to the slider, and the settings button is clipped to ...ings:
The final layout moved capture and settings into their own row and dropped the toggle, since the camera type now lives in the sheet:
The green cast
RGB captures came back with a strong green tint. It was obvious in the file browser and even more obvious on a white reference surface.
The tint was in the saved JPEG, not only in the preview, so the pipeline was writing the wrong colours to disk. A capture on the same phone with the native camera app, a minute later, was neutral. So the cause was the capture configuration, not the sensor or the scene.
The cast came from the manual white balance control I was adding to the sheet. The locked gains the app applied while I was adjusting it were wrong, and every capture after that inherited them.
The fix was to default to continuous auto white balance, and when a fixed mode is chosen, to compute the gains from a preset and clamp them to the device maximum before locking:
try device.lockForConfiguration()
if device.isWhiteBalanceModeSupported(.continuousAutoWhiteBalance) {
device.whiteBalanceMode = .continuousAutoWhiteBalance
}
// or, for a fixed reference card at the start of a session
if device.isWhiteBalanceModeSupported(.locked) {
let gains = device.deviceWhiteBalanceGains(for: .daylight)
let maxGain = device.maxWhiteBalanceGain
let clamped = AVCaptureDevice.WhiteBalanceGains(
redGain: min(gains.redGain, maxGain),
greenGain: min(gains.greenGain, maxGain),
blueGain: min(gains.blueGain, maxGain)
)
device.setWhiteBalanceModeLocked(with: clamped)
}
device.unlockForConfiguration()
I kept the control in the sheet. Locking white balance against a reference card at the start of a session is more repeatable across plots than auto white balance, which helps when RGB is analyzed together with thermal and depth.
The two files side by side:
What a capture costs in storage
At full still resolution each JPEG is 3024×4032 and about 2.8 MB:
At 0.10 s a plot walk generates a few hundred megabytes, and the phone has to write them while the thermal stream is still running. So the interval also controls storage and write load.
Still crashing in the thermal path
The 2025 build still crashed, and the reports pointed at the thermal SDK again, this time inside vendor code with no symbols:
ThermalSDK at an unresolved offset.The practical workaround came from FLIR’s own app rather than from documentation. The FLIR ONE app exposes two modes:
FLIR’s support page describes the trade as “Quality Mode for best possible image quality, or Performance Mode for a more responsive camera experience”. Their app can switch because it does not also have to hold depth frames, write TIFFs, and read a BLE weather station on the same queue. At a 0.10 s interval our app asks the thermal SDK for stills at that rate, which the pipeline cannot sustain. The workaround is to keep the interval longer.
One item is still open: add a weather station connection flag to the JSON. Right now a missing reading and a zero reading look identical in the metadata.
References
- Apple Developer: AVCaptureDevice (white balance modes and gains)
- Apple Developer: AVCaptureDevice.WhiteBalanceMode
- Teledyne FLIR: FLIR Technical Support Center, on Quality and Performance Mode in the FLIR ONE app
- Teledyne FLIR: FLIR Atlas Mobile SDK downloads and documentation
- Apple Developer: UserDefaults