[BUG] CameraView on iOS: camera preview is not always rotated correct and photos are rotated
Is there an existing issue for this?
- [x] I have searched the existing issues
Did you read the "Reporting a bug" section on Contributing file?
- [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
Current Behavior
Mind that there is a similar bug reported on Android (#2096). The issue described here is iOS specific and has a different cause.
The camera preview of the CameraView is not always rotated correct. Photos taken in landscape mode are shown rotated sideways. Photos taken in portrait-upside-down are shown upside down.
Image 1:
Image 2 a and b:
Image 3:
Image 4:
Expected Behavior
The camera preview of the CameraView should always rotate correct. Photos taken should be shown correct.
Steps To Reproduce
1 - Build and run on iOS: https://github.com/CommunityToolkit/Maui/blob/main/samples/CommunityToolkit.Maui.Sample.sln 2 - Select Views => CameraView Page 3 - When the camera was activated previously in portrait mode and you started the app in landscape mode, the camera preview is shown sideways (image 1). 4 - When you rotate the device quickly 180 degrees, the camera preview will be shown upside down (image 2a/b). 5. When you take a picture in landscape mode, the picture is shown sideways (image 3) 6. When you take a picture in portrait-upside-down the picture is shown upside down (image 4).
Link to public reproduction project repository
https://github.com/CommunityToolkit/Maui/
Environment
- .NET MAUI CommunityToolkit.Maui.Camera: 1.0.5
- OS: iOS 18.0.1 and iOS 17.6.1
- .NET MAUI: 8.0.92
Anything else?
The Camera preview is not always correct because UIDevice.CurrentDevice.Orientation does not always return the correct rotation. In recent iOS versions the UIWindowScene from UIApplication.SharedApplication.ConnectedScenes should be used. Also, when rotating from Landscape right to Landscape left (or portrait to portrait upside down) PreviewView.LayoutSubviews will not be triggered.
The photos are shown rotated, because the VideoOrientation of the AVCapturePhotoOutput connection is not set correctly when taking a picture.
I created a fix to solve this: https://github.com/Forestbrook/CommunityToolkit.Maui/blob/Fix-iOS-photo-orientation/src/CommunityToolkit.Maui.Camera/CameraManager.macios.cs I will create a PR with this fix.
Related bug 1: #2096 CamaraView Photo Rotate: almost the same issue on Android. My fix is for iOS, so it won't solve this bug.
Related bug 2: #2064 CameraView always sets upper-right to EXIF orientation tag on iOS. My solution might solve this bug, please test and confirm after implementation.
The preview orientation updates but the orientation of the captured image is not consistent with the preview. Occasionally the captured photo will be upside down from what's seen in the preview.
Hi. I am seeing a related issue where my app goes into landscape and the camera preview is portrait. You have to turn the device to the other landscape position to get it to sync. I see that there are PRs to fix but I'm not sure of the status. I have a project targeting .net8 and one targeting .net9. Both have the most up to date CommunityToolkit.Maui packages they can as of this writing and both have the issue. Is there something else I need to do?