react-native-vision-camera
react-native-vision-camera copied to clipboard
fix: orientation on iOS
What
Upon testing the new orientation implementation, I found some issues on iOS.
On iOS 17:
- onPreviewOrientationChanged and onOutputOrientationChanged do not correctly return the orientation. For example, when the phone is in portrait mode, the returned mode is "landscape-left".
On iOS < 17:
- The video orientation is not good. The recorded video is not rotated correctly (it doesn't use the camera sensor orientation).
- When the orientation is locked on the phone, the output orientation is not updated (e.g. onOutputOrientationChanged / onUIRotationChanged).
Upon analyzing the code, I realized that depending on the iOS version, we use videoRotationAngleForHorizonLevelPreview/videoRotationAngleForHorizonLevelCapture and on the other side UIDevice.current.orientation/UIApplication.shared.statusBarOrientation. videoRotationAngleForHorizonLevelPreview/videoRotationAngleForHorizonLevelCapture already takes into account the camera orientation, whereas the other solution takes into account the phone orientation.
Additionally, when portrait orientation lock is on, UIDevice.current.orientation always returns .up. Therefore, I had to modify the LegacyCameraOrientationCoordinator to function correctly even when the orientation is locked (to achieve the behavior described in the documentation and the same as with iOS 17).
Changes
- Fix preview / output orientation (same on every iOS version)
- Usage of the accelerometer in LegacyCameraOrientationCoordinator to support locked orientation.
Tested on
- iPhone 12, iOS 17.4.1
- iPad Air 2, iOS 15.8.2
- iPod, iOS 12.5.7