CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.27k stars 397 forks source link

[BUG] CameraView on iOS: camera preview is not always rotated correct and photos are rotated #2311

Open Forestbrook opened 4 days ago

Forestbrook commented 4 days ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

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

Image 2 a and b: Image Image

Image 3: Image

Image 4: Image

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).

  1. When you take a picture in landscape mode, the picture is shown sideways (image 3)
  2. 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

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.

Forestbrook commented 4 days ago

PR: Fix bug CameraView on iOS: camera preview is not always rotated correct and photos are rotated