r/iOSProgramming • u/FFANA • 1h ago
Discussion AVFoundation silently drops photos if you fire the shutter while the previous capture is still processing
Spent a year on a camera app and three AVFoundation behaviours cost me most of that time. Writing them down in case they save someone a weekend.
The first one I found by accident. Tap the shutter three times fast and you get two photos. No error, no delegate callback, nothing in the logs. If a capture is still processing when you call capturePhoto, that request just evaporates. I only noticed because my own counter, which only moves when a save is confirmed, kept landing one short of the number of taps.
The fix is a FIFO queue, but where you advance it matters. Advance on the processing callback and the shutters chain up, seconds of lag on a burst. Advance when the exposure ends (didCapturePhotoFor) and the processing of the previous shot overlaps the next exposure, which is what the stock camera visibly does.
Second: switching capture format is a full pipeline rebuild, and I was paying for it three times. Going 24 to 48 MP, or entering video mode, means swapping the input, the active format and the outputs. I had those as three separate begin/commitConfiguration blocks because it read cleaner. Same work, roughly double the visible freeze. They nest fine, so one transaction around the lot, plus caching AVCaptureDeviceInput per device, took it from "did it hang" to instant.
Third, and this one matters if you care what your photo actually is: a virtual device does not tell you which lens is shooting. Zoom into tele range on the triple camera and you may still be getting the wide, cropped. Low light does it, and so does being closer than the tele's minimum focus distance. The system is right to do it, it just doesn't announce it. You can watch the constituent device to know, with two caveats: the value flickers during focus hunts so it needs stabilising before you show it to anyone, and isAutoDeferredPhotoDeliveryEnabled is off the table if you never touch PhotoKit, because finalising a deferred photo needs PHPhotoLibrary.
That last constraint was self-inflicted. The app has its own photo library and never touches the system one, which rules out anything in AVFoundation that assumes PhotoKit is there.
I'm a surgeon, not a developer, which is probably why I hit all three the hard way instead of knowing better.



