Error on first photo captured after device orientation change

I am using AVFoundation to capture a photo. This was all working fine, then I realized all the photos were saving to the photo library in portrait mode. I wanted them to save in the orientation the device was in when the camera took the picture, much as the built in camera app does on iOS. So I added this code:

if let videoConnection = photoOutput.connection(with: .video),
   videoConnection.isVideoOrientationSupported {
    
    // From() is just a helper to get video orientations from the device orientation. 
    videoConnection.videoOrientation = .from(UIDevice.current.orientation)
    print("Photo orientation set to \(videoConnection.videoOrientation).")
}

With this addition, the first photo taken after a device rotation logs this error in the debugger:

<<<< FigCaptureSessionRemote >>>> Fig assert: "err == 0 " at bail (FigCaptureSessionRemote.m:866) - (err=-12784)

Subsequent photos will not repeat the error. Once you rotate the device again, same behavior. Photos taken after the app loads, but before any rotations have been made, do not produce this error.

I have tried many things, no dice. If I comment this code out it works without error, but of course the photos are all saved in portrait mode again.

I had the same issue.

<<<< FigCaptureSessionRemote >>>> Fig assert: "err == 0 " at bail (FigCaptureSessionRemote.m:866) - (err=-12784)

The problem was that, just like in your code, setting the orientation at the time of capture was the cause. This value should not be set at capture time; instead, it changes with device orientation. By monitoring the AVCaptureDevice.RotationCoordinator’s videoRotationAngleForHorizonLevelCapture property and updating the value accordingly, I was able to resolve the issue.

The Apple sample code AVCam is a useful reference for this.

https://developer.apple.com/documentation/avfoundation/capture_setup/avcam_building_a_camera_app

Error on first photo captured after device orientation change
 
 
Q