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

Commit 3b6b831

Browse files
authored
[CameraView] [iOS] Flash does not work fix (#1563)
* Update CameraViewRenderer.ios.cs * Update FormsCameraView.ios.cs * Update FormsCameraView.ios.cs * Brushed up sample
1 parent a09a14f commit 3b6b831

File tree

4 files changed

+56
-23
lines changed

4 files changed

+56
-23
lines changed

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

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,44 @@
66
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
77
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
88
<ScrollView>
9-
<StackLayout>
10-
<xct:CameraView
11-
x:Name="cameraView"
12-
CaptureMode="Video"
13-
FlashMode="On"
14-
HorizontalOptions="FillAndExpand"
15-
MediaCaptured="CameraView_MediaCaptured"
16-
OnAvailable="CameraView_OnAvailable"
17-
VerticalOptions="FillAndExpand" />
9+
<Grid RowDefinitions="300, Auto">
10+
<Grid ColumnDefinitions="*, *" Grid.Row="0">
11+
<xct:CameraView
12+
Grid.Column="0"
13+
x:Name="cameraView"
14+
CaptureMode="Photo"
15+
FlashMode="Off"
16+
HorizontalOptions="FillAndExpand"
17+
MediaCaptured="CameraView_MediaCaptured"
18+
OnAvailable="CameraView_OnAvailable"
19+
VerticalOptions="FillAndExpand" />
20+
<Label
21+
Grid.Column="0"
22+
Text="Camera"
23+
HorizontalTextAlignment="Center"
24+
HorizontalOptions="FillAndExpand"
25+
VerticalOptions="End" />
26+
27+
<Image
28+
Grid.Column="1"
29+
x:Name="previewPicture"
30+
Aspect="AspectFit"
31+
BackgroundColor="LightGray" />
32+
33+
<xct:MediaElement
34+
Grid.Column="1"
35+
x:Name="previewVideo"
36+
Aspect="AspectFit"
37+
BackgroundColor="LightGray"
38+
IsVisible="false"/>
39+
<Label
40+
Grid.Column="1"
41+
Text="Result"
42+
HorizontalTextAlignment="Center"
43+
HorizontalOptions="FillAndExpand"
44+
VerticalOptions="End" />
45+
</Grid>
46+
1847

1948
<StackLayout Grid.Row="1" Orientation="Horizontal">
2049
<Label x:Name="zoomLabel" />
@@ -29,7 +58,7 @@
2958
Value="1" />
3059
</StackLayout>
3160

32-
<StackLayout>
61+
<StackLayout Grid.Row="2">
3362
<Grid ColumnDefinitions="*, *" RowDefinitions="*,*">
3463
<StackLayout
3564
Grid.Row="0"
@@ -38,7 +67,7 @@
3867
Orientation="Horizontal">
3968
<Switch
4069
Margin="0,0,5,0"
41-
IsToggled="True"
70+
IsToggled="False"
4271
Toggled="VideoSwitch_Toggled" />
4372
<Label Text="Video" />
4473
</StackLayout>
@@ -60,24 +89,19 @@
6089
Orientation="Horizontal">
6190
<Switch
6291
Margin="0,0,5,0"
63-
IsToggled="True"
92+
IsToggled="False"
6493
Toggled="FlashSwitch_Toggled" />
6594
<Label Text="Flash" />
6695
</StackLayout>
6796
</Grid>
6897

6998
<Button
7099
x:Name="doCameraThings"
71-
Command="{Binding ShutterCommand, Source={x:Reference cameraView}}"
100+
Clicked="DoCameraThings_Clicked"
72101
IsEnabled="False"
73-
Text="Start Recording" />
74-
<Image
75-
x:Name="previewPicture"
76-
Aspect="AspectFit"
77-
BackgroundColor="LightGray"
78-
HeightRequest="250"
79-
IsVisible="False" />
102+
Text="Snap picture" />
103+
80104
</StackLayout>
81-
</StackLayout>
105+
</Grid>
82106
</ScrollView>
83107
</pages:BasePage>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,16 @@ void CameraView_MediaCaptured(object? sender, MediaCapturedEventArgs e)
7474
default:
7575
case CameraCaptureMode.Default:
7676
case CameraCaptureMode.Photo:
77+
previewVideo.IsVisible = false;
7778
previewPicture.IsVisible = true;
7879
previewPicture.Rotation = e.Rotation;
7980
previewPicture.Source = e.Image;
8081
doCameraThings.Text = "Snap Picture";
8182
break;
8283
case CameraCaptureMode.Video:
8384
previewPicture.IsVisible = false;
85+
previewVideo.IsVisible = true;
86+
previewVideo.Source = e.Video;
8487
doCameraThings.Text = "Start Recording";
8588
break;
8689
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ protected override void OnElementChanged(ElementChangedEventArgs<CameraView> e)
2929
Control.Available += OnAvailability;
3030
Control.FinishCapture += FinishCapture;
3131

32-
Control.SwitchFlash(Element.FlashMode);
3332
Control.SetBounds(Element.WidthRequest, Element.HeightRequest);
3433
Control.VideoStabilization = Element.VideoStabilization;
3534
Control.Zoom = (float)Element.Zoom;
3635
Control.RetrieveCameraDevice(Element.CameraOptions);
36+
Control.SwitchFlash(Element.FlashMode);
3737
}
3838

3939
if (e.OldElement != null)

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,13 @@ public async Task TakePhoto()
203203
photoOutputConnection.VideoOrientation = previewLayer.Connection?.VideoOrientation ?? throw new NullReferenceException();
204204

205205
var photoSettings = AVCapturePhotoSettings.Create();
206-
photoSettings.FlashMode = (AVCaptureFlashMode)flashMode;
206+
207+
// Torch is set somewhere different
208+
if (flashMode != CameraFlashMode.Torch)
209+
{
210+
photoSettings.FlashMode = (AVCaptureFlashMode)flashMode;
211+
}
212+
207213
photoSettings.IsHighResolutionPhotoEnabled = true;
208214

209215
var photoCaptureDelegate = new PhotoCaptureDelegate

0 commit comments

Comments
 (0)