@@ -13,10 +13,8 @@ namespace CommunityToolkit.Maui.Views;
1313[ SupportedOSPlatform ( "android21.0" ) ]
1414[ SupportedOSPlatform ( "ios" ) ]
1515[ SupportedOSPlatform ( "maccatalyst" ) ]
16- public partial class CameraView : View , ICameraView
16+ public partial class CameraView : View , ICameraView , IDisposable
1717{
18- readonly SemaphoreSlim captureImageSemaphoreSlim = new ( 1 , 1 ) ;
19-
2018 static readonly BindablePropertyKey isAvailablePropertyKey =
2119 BindableProperty . CreateReadOnly ( nameof ( IsAvailable ) , typeof ( bool ) , typeof ( CameraView ) , CameraViewDefaults . IsAvailable ) ;
2220
@@ -81,8 +79,12 @@ public partial class CameraView : View, ICameraView
8179 public static readonly BindableProperty StopCameraPreviewCommandProperty =
8280 BindableProperty . CreateReadOnly ( nameof ( StopCameraPreviewCommand ) , typeof ( ICommand ) , typeof ( CameraView ) , null , BindingMode . OneWayToSource , defaultValueCreator : CameraViewDefaults . CreateStopCameraPreviewCommand ) . BindableProperty ;
8381
82+
83+ readonly SemaphoreSlim captureImageSemaphoreSlim = new ( 1 , 1 ) ;
8484 readonly WeakEventManager weakEventManager = new ( ) ;
8585
86+ bool isDisposed ;
87+
8688 /// <summary>
8789 /// Event that is raised when the camera capture fails.
8890 /// </summary>
@@ -192,6 +194,13 @@ bool ICameraView.IsBusy
192194
193195 new CameraViewHandler Handler => ( CameraViewHandler ) ( base . Handler ?? throw new InvalidOperationException ( "Unable to retrieve Handler" ) ) ;
194196
197+ /// <inheritdoc/>
198+ public void Dispose ( )
199+ {
200+ Dispose ( disposing : true ) ;
201+ GC . SuppressFinalize ( this ) ;
202+ }
203+
195204 /// <inheritdoc cref="ICameraView.GetAvailableCameras"/>
196205 public async ValueTask < IReadOnlyList < CameraInfo > > GetAvailableCameras ( CancellationToken token )
197206 {
@@ -249,6 +258,20 @@ public Task StartCameraPreview(CancellationToken token) =>
249258 public void StopCameraPreview ( ) =>
250259 Handler . CameraManager . StopCameraPreview ( ) ;
251260
261+ /// <inheritdoc/>
262+ protected virtual void Dispose ( bool disposing )
263+ {
264+ if ( ! isDisposed )
265+ {
266+ if ( disposing )
267+ {
268+ captureImageSemaphoreSlim . Dispose ( ) ;
269+ }
270+
271+ isDisposed = true ;
272+ }
273+ }
274+
252275 void ICameraView . OnMediaCaptured ( Stream imageData )
253276 {
254277 weakEventManager . HandleEvent ( this , new MediaCapturedEventArgs ( imageData ) , nameof ( MediaCaptured ) ) ;
0 commit comments