Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public sealed class FormsCameraView : UIView, IAVCaptureFileOutputRecordingDeleg
bool isAvailable;
bool isDisposed;
CameraFlashMode flashMode;
PhotoCaptureDelegate? photoCaptureDelegate;
readonly float imgScale = 1f;

public event EventHandler<bool>? Busy;
Expand Down Expand Up @@ -198,25 +199,19 @@ public async Task TakePhoto()
// iOS >= 10
if (photoOutput != null)
{
var photoOutputConnection = photoOutput.ConnectionFromMediaType(AVMediaType.Video);
if (photoOutputConnection != null)
photoOutputConnection.VideoOrientation = previewLayer.Connection?.VideoOrientation ?? throw new NullReferenceException();

var photoSettings = AVCapturePhotoSettings.Create();
photoSettings.FlashMode = GetFlashMode();
photoSettings.IsHighResolutionPhotoEnabled = true;

var photoCaptureDelegate = new PhotoCaptureDelegate
try
{
OnFinishCapture = (data, error) =>
{
FinishCapture?.Invoke(this, new Tuple<NSObject?, NSError?>(data, error));
IsBusy = false;
},
WillCapturePhotoAnimation = () => Animate(0.25, () => previewLayer.Opacity = 1)
};
var photoOutputConnection = photoOutput.ConnectionFromMediaType(AVMediaType.Video);
if (photoOutputConnection != null)
photoOutputConnection.VideoOrientation = previewLayer.Connection?.VideoOrientation ?? throw new NullReferenceException();

photoOutput.CapturePhoto(photoSettings, photoCaptureDelegate);
photoOutput.CapturePhoto(GetCapturePhotoSettings(), GetPhotoCaptureDelegate());
}
catch (Exception)
{
FinishCapture?.Invoke(this, new Tuple<NSObject?, NSError?>(null, new NSError(new NSString("Failed to create image"), 0)));
IsBusy = false;
}
return;
}

Expand All @@ -231,14 +226,39 @@ public async Task TakePhoto()
}
catch (Exception)
{
FinishCapture?.Invoke(this, new Tuple<NSObject?, NSError?>(null, new NSError(new NSString("faled create image"), 0)));
FinishCapture?.Invoke(this, new Tuple<NSObject?, NSError?>(null, new NSError(new NSString("Failed to create image"), 0)));
}
finally
{
IsBusy = false;
}
}

AVCapturePhotoSettings GetCapturePhotoSettings()
{
var photoSettings = AVCapturePhotoSettings.Create();
photoSettings.FlashMode = GetFlashMode();
photoSettings.IsHighResolutionPhotoEnabled = true;
return photoSettings;
}

PhotoCaptureDelegate GetPhotoCaptureDelegate()
{
if (photoCaptureDelegate == null)
{
photoCaptureDelegate = new PhotoCaptureDelegate
{
OnFinishCapture = (data, error) =>
{
FinishCapture?.Invoke(this, new Tuple<NSObject?, NSError?>(data, error));
IsBusy = false;
},
WillCapturePhotoAnimation = () => Animate(0.25, () => previewLayer.Opacity = 1)
};
}
return photoCaptureDelegate;
}

AVCaptureFlashMode GetFlashMode()
{
switch (flashMode)
Expand Down Expand Up @@ -564,6 +584,9 @@ void ClearCaptureSession()
captureSession.RemoveInput(input);
}

photoCaptureDelegate?.Dispose();
photoCaptureDelegate = null;

input?.Dispose();
input = null;

Expand Down