Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 05e54fb

Browse files
GUOLDEVjfversluis
andauthored
Fixed CameraView Video Orientation (#1583)
Co-authored-by: Gerald Versluis <[email protected]>
1 parent db37df1 commit 05e54fb

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

samples/XCT.Sample/Pages/Views/CameraViewPage.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
77
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
88
<ScrollView>
9-
<Grid RowDefinitions="300, Auto">
9+
<Grid RowDefinitions="300, Auto, *">
1010
<Grid ColumnDefinitions="*, *" Grid.Row="0">
1111
<xct:CameraView
1212
Grid.Column="0"
@@ -43,7 +43,6 @@
4343
HorizontalOptions="FillAndExpand"
4444
VerticalOptions="End" />
4545
</Grid>
46-
4746

4847
<StackLayout Grid.Row="1" Orientation="Horizontal">
4948
<Label x:Name="zoomLabel" />

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/CameraView/iOS/CameraViewRenderer.ios.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,12 @@ protected override void OnElementPropertyChanged(object? sender, System.Componen
183183
case nameof(CameraView.CameraOptions):
184184
Control.RetrieveCameraDevice(Element.CameraOptions);
185185
break;
186+
case nameof(Element.Height):
187+
case nameof(Element.Width):
186188
case nameof(VisualElement.HeightProperty):
187189
case nameof(VisualElement.WidthProperty):
188190
Control.SetBounds(Element.Width, Element.Height);
191+
Control.SetOrientation();
189192
break;
190193
case nameof(CameraView.VideoStabilization):
191194
Control.VideoStabilization = Element.VideoStabilization;

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/CameraView/iOS/FormsCameraView.ios.cs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,54 @@ public FormsCameraView()
6161
AddConstraints(NSLayoutConstraint.FromVisualFormat("H:|[mainView]|", NSLayoutFormatOptions.AlignAllTop, null, new NSDictionary("mainView", mainView)));
6262
}
6363

64-
void SetStartOrientation()
64+
internal void SetOrientation()
6565
{
66-
var previewLayerFrame = previewLayer.Frame;
66+
SetFrameOrientation();
67+
SetVideoOrientation();
68+
}
6769

68-
switch (UIApplication.SharedApplication.StatusBarOrientation)
70+
void SetFrameOrientation()
71+
{
72+
try
6973
{
70-
case UIInterfaceOrientation.Portrait:
71-
case UIInterfaceOrientation.PortraitUpsideDown:
72-
previewLayerFrame.Height = UIScreen.MainScreen.Bounds.Height;
73-
previewLayerFrame.Width = UIScreen.MainScreen.Bounds.Width;
74-
break;
75-
76-
case UIInterfaceOrientation.LandscapeLeft:
77-
case UIInterfaceOrientation.LandscapeRight:
78-
previewLayerFrame.Width = UIScreen.MainScreen.Bounds.Width;
79-
previewLayerFrame.Height = UIScreen.MainScreen.Bounds.Height;
80-
break;
74+
var previewLayerFrame = previewLayer.Frame;
75+
previewLayerFrame.Height = mainView.Bounds.Height;
76+
previewLayerFrame.Width = mainView.Bounds.Width;
77+
previewLayer.Frame = previewLayerFrame;
78+
}
79+
catch (Exception error)
80+
{
81+
LogError("Failed to adjust frame", error);
8182
}
83+
}
8284

85+
void SetVideoOrientation()
86+
{
8387
try
8488
{
85-
previewLayer.Frame = previewLayerFrame;
89+
if (previewLayer?.Connection?.SupportsVideoOrientation == true)
90+
previewLayer.Connection.VideoOrientation = GetVideoOrientation();
8691
}
8792
catch (Exception error)
8893
{
89-
LogError("Failed to adjust frame", error);
94+
LogError("Failed to set video orientation", error);
95+
}
96+
}
97+
98+
AVCaptureVideoOrientation GetVideoOrientation()
99+
{
100+
switch (UIApplication.SharedApplication.StatusBarOrientation)
101+
{
102+
case UIInterfaceOrientation.Portrait:
103+
return AVCaptureVideoOrientation.Portrait;
104+
case UIInterfaceOrientation.PortraitUpsideDown:
105+
return AVCaptureVideoOrientation.PortraitUpsideDown;
106+
case UIInterfaceOrientation.LandscapeLeft:
107+
return AVCaptureVideoOrientation.LandscapeLeft;
108+
case UIInterfaceOrientation.LandscapeRight:
109+
return AVCaptureVideoOrientation.LandscapeRight;
110+
default:
111+
throw new ArgumentOutOfRangeException(nameof(UIApplication.SharedApplication.StatusBarOrientation));
90112
}
91113
}
92114

@@ -555,8 +577,7 @@ void InitializeCamera()
555577

556578
InvokeOnMainThread(() =>
557579
{
558-
captureConnection = previewLayer.Connection;
559-
SetStartOrientation();
580+
SetOrientation();
560581
captureSession.StartRunning();
561582
});
562583
}

0 commit comments

Comments
 (0)