@@ -104,22 +104,21 @@ public async partial ValueTask UpdateCaptureResolution(Size resolution, Cancella
104104 cameraView . SelectedCamera = cameraProvider . AvailableCameras ? . FirstOrDefault ( ) ?? throw new CameraException ( "No camera available on device" ) ;
105105 }
106106
107- var filteredFormatList = cameraView . SelectedCamera . SupportedFormats . Where ( f =>
108- {
109- var d = ( ( CMVideoFormatDescription ) f . FormatDescription ) . Dimensions ;
110- return d . Width <= resolution . Width && d . Height <= resolution . Height ;
111- } ) . ToList ( ) ;
107+ var formatsMatchingResolution = cameraView . SelectedCamera . SupportedFormats
108+ . Where ( format => MatchesResolution ( format , resolution ) )
109+ . ToList ( ) ;
112110
113- filteredFormatList = [ .. ( filteredFormatList . Count is not 0 ? filteredFormatList : cameraView . SelectedCamera . SupportedFormats )
114- . OrderByDescending ( f =>
115- {
116- var d = ( ( CMVideoFormatDescription ) f . FormatDescription ) . Dimensions ;
117- return d . Width * d . Height ;
118- } ) ] ;
111+ var availableFormats = formatsMatchingResolution . Count is not 0
112+ ? formatsMatchingResolution
113+ : GetPhotoCompatibleFormats ( cameraView . SelectedCamera . SupportedFormats ) ;
114+
115+ var selectedFormat = availableFormats
116+ . OrderByDescending ( f => f . ResolutionArea )
117+ . FirstOrDefault ( ) ;
119118
120- if ( filteredFormatList . Count is not 0 )
119+ if ( selectedFormat is not null )
121120 {
122- captureDevice . ActiveFormat = filteredFormatList . First ( ) ;
121+ captureDevice . ActiveFormat = selectedFormat ;
123122 }
124123
125124 captureDevice . UnlockForConfiguration ( ) ;
@@ -460,6 +459,24 @@ void UpdateVideoOrientation()
460459 previewView ? . UpdatePreviewVideoOrientation ( videoOrientation ) ;
461460 }
462461
462+ IEnumerable < AVCaptureDeviceFormat > GetPhotoCompatibleFormats ( IEnumerable < AVCaptureDeviceFormat > formats )
463+ {
464+ if ( photoOutput is not null )
465+ {
466+ var photoPixelFormats = photoOutput . GetSupportedPhotoPixelFormatTypesForFileType ( nameof ( AVFileTypes . Jpeg ) ) ;
467+ return formats . Where ( format => photoPixelFormats . Contains ( ( NSNumber ) format . FormatDescription . MediaSubType ) ) ;
468+ }
469+
470+ return formats ;
471+ }
472+
473+ static bool MatchesResolution ( AVCaptureDeviceFormat format , Size resolution )
474+ {
475+ var dimensions = ( ( CMVideoFormatDescription ) format . FormatDescription ) . Dimensions ;
476+ return dimensions . Width <= resolution . Width
477+ && dimensions . Height <= resolution . Height ;
478+ }
479+
463480 sealed class AVCapturePhotoCaptureDelegateWrapper : AVCapturePhotoCaptureDelegate
464481 {
465482 readonly TaskCompletionSource < CapturePhotoResult > taskCompletionSource = new ( ) ;
0 commit comments