|
|
@@ -1,13 +1,21 @@
|
|
|
+using System;
|
|
|
using System.Drawing;
|
|
|
+using System.IO;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
+using System.Threading.Tasks;
|
|
|
using Avalonia.Controls;
|
|
|
using AVFoundation;
|
|
|
+using CoreGraphics;
|
|
|
using CoreMedia;
|
|
|
+using Foundation;
|
|
|
using InABox.Core;
|
|
|
using Microsoft.Maui.ApplicationModel;
|
|
|
+using Microsoft.Maui.Storage;
|
|
|
+using MobileCoreServices;
|
|
|
+using UIKit;
|
|
|
|
|
|
namespace InABox.Avalonia.Platform.iOS
|
|
|
{
|
|
|
-
|
|
|
public class iOS_ImageTools : IImageTools
|
|
|
{
|
|
|
public Logger Logger { get; set; }
|
|
|
@@ -70,58 +78,58 @@ namespace InABox.Avalonia.Platform.iOS
|
|
|
return await MainThread.InvokeOnMainThreadAsync(async () =>
|
|
|
await InternalGetPhotoAsync<Permissions.Camera>(UIImagePickerControllerSourceType.Camera, null, null));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private async Task<ImageFile> InternalGetPhotoAsync<TPermission>(UIImagePickerControllerSourceType source, int? compression, Size? constraints)
|
|
|
where TPermission : Permissions.BasePermission, new()
|
|
|
{
|
|
|
var taskCompletionSource = new TaskCompletionSource<ImageFile>();
|
|
|
- // if (await Permissions.RequestAsync<TPermission>() == PermissionStatus.Granted)
|
|
|
- // {
|
|
|
- // var imagePicker = new UIImagePickerController
|
|
|
- // {
|
|
|
- // SourceType = source,
|
|
|
- // MediaTypes = new string[] { UTType.Image }
|
|
|
- // };
|
|
|
- //
|
|
|
- // // Struggling to get IOS define to enable Platform shared code, which is a bit shitty
|
|
|
- // var viewController = WindowStateManager.Default.GetCurrentUIViewController(true); // Platform.getCurrentUIViewController();
|
|
|
- //
|
|
|
- // imagePicker.AllowsEditing = false;
|
|
|
- // imagePicker.FinishedPickingMedia += async (sender, e) =>
|
|
|
- // {
|
|
|
- // var jpegFilename = Path.Combine(FileSystem.CacheDirectory, $"{Guid.NewGuid()}.jpg");
|
|
|
- // var source = e.Info[UIImagePickerController.OriginalImage] as UIImage;
|
|
|
- // var rotated = AutoRotateImage(source);
|
|
|
- // var scaled = ScaleImage(rotated, constraints);
|
|
|
- // var result = scaled.AsJPEG(new NFloat(compression ?? 100)/100);
|
|
|
- // await viewController.DismissViewControllerAsync(true);
|
|
|
- // if (result.Save(jpegFilename, false, out var error))
|
|
|
- // {
|
|
|
- // taskCompletionSource.TrySetResult(new FileResult(jpegFilename));
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // taskCompletionSource.TrySetException(new Exception($"Error saving the image: {error}"));
|
|
|
- // }
|
|
|
- // imagePicker?.Dispose();
|
|
|
- // imagePicker = null;
|
|
|
- // };
|
|
|
- //
|
|
|
- // imagePicker.Canceled += async (sender, e) =>
|
|
|
- // {
|
|
|
- // await viewController.DismissViewControllerAsync(true);
|
|
|
- // taskCompletionSource.TrySetResult(null);
|
|
|
- // imagePicker?.Dispose();
|
|
|
- // imagePicker = null;
|
|
|
- // };
|
|
|
- //
|
|
|
- // await viewController.PresentViewControllerAsync(imagePicker, true);
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
+ if (await Permissions.RequestAsync<TPermission>() == PermissionStatus.Granted)
|
|
|
+ {
|
|
|
+ var imagePicker = new UIImagePickerController
|
|
|
+ {
|
|
|
+ SourceType = source,
|
|
|
+ MediaTypes = new string[] { UTType.Image }
|
|
|
+ };
|
|
|
+
|
|
|
+ // Struggling to get IOS define to enable Platform shared code, which is a bit shitty
|
|
|
+ var viewController = ViewControllerHelper.GetVisibleViewController();
|
|
|
+
|
|
|
+ imagePicker.AllowsEditing = false;
|
|
|
+ imagePicker.FinishedPickingMedia += async (sender, e) =>
|
|
|
+ {
|
|
|
+ var jpegFilename = Path.Combine(FileSystem.CacheDirectory, $"{Guid.NewGuid()}.jpg");
|
|
|
+ var source = e.Info[UIImagePickerController.OriginalImage] as UIImage;
|
|
|
+ var rotated = AutoRotateImage(source);
|
|
|
+ var scaled = ScaleImage(rotated, constraints);
|
|
|
+ var result = scaled.AsJPEG(new NFloat(compression ?? 100)/100);
|
|
|
+ await viewController.DismissViewControllerAsync(true);
|
|
|
+ if (result.Save(jpegFilename, false, out var error))
|
|
|
+ {
|
|
|
+ taskCompletionSource.TrySetResult(new ImageFile(jpegFilename, result.ToArray()));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ taskCompletionSource.TrySetException(new Exception($"Error saving the image: {error}"));
|
|
|
+ }
|
|
|
+ imagePicker?.Dispose();
|
|
|
+ imagePicker = null;
|
|
|
+ };
|
|
|
+
|
|
|
+ imagePicker.Canceled += async (sender, e) =>
|
|
|
+ {
|
|
|
+ await viewController.DismissViewControllerAsync(true);
|
|
|
+ taskCompletionSource.TrySetResult(null);
|
|
|
+ imagePicker?.Dispose();
|
|
|
+ imagePicker = null;
|
|
|
+ };
|
|
|
+
|
|
|
+ await viewController.PresentViewControllerAsync(imagePicker, true);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
taskCompletionSource.TrySetResult(null);
|
|
|
taskCompletionSource.TrySetException(new PermissionException("Camera permission not granted"));
|
|
|
- //}
|
|
|
+ }
|
|
|
|
|
|
return await taskCompletionSource.Task;
|
|
|
}
|