-
Notifications
You must be signed in to change notification settings - Fork 11
/
PlatformImageCropper.cs
83 lines (71 loc) · 3.21 KB
/
PlatformImageCropper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Bind_TOCropViewController;
using CoreGraphics;
using Foundation;
using System.Diagnostics;
using UIKit;
namespace ImageCropper.Maui
{
// All the code in this file is only included on iOS.
public class PlatformImageCropper : IImageCropperWrapper
{
public void ShowFromFile(ImageCropper imageCropper, string imageFile)
{
UIImage image = UIImage.FromFile(imageFile);
TOCropViewController cropViewController;
if (imageCropper.CropShape == ImageCropper.CropShapeType.Oval)
{
cropViewController = new TOCropViewController(TOCropViewCroppingStyle.Circular, image);
}
else
{
cropViewController = new TOCropViewController(image);
}
cropViewController.DoneButtonTitle = imageCropper.CropButtonTitle;
cropViewController.CancelButtonTitle = imageCropper.CancelButtonTitle;
if (imageCropper.AspectRatioX > 0 && imageCropper.AspectRatioY > 0)
{
cropViewController.AspectRatioPreset = TOCropViewControllerAspectRatioPreset.Custom;
cropViewController.ResetAspectRatioEnabled = false;
cropViewController.AspectRatioLockEnabled = true;
cropViewController.CustomAspectRatio = new CGSize(imageCropper.AspectRatioX, imageCropper.AspectRatioY);
}
cropViewController.OnDidCropToRect = (outImage, cropRect, angle) =>
{
Finalize(imageCropper, outImage);
};
cropViewController.OnDidCropToCircleImage = (outImage, cropRect, angle) =>
{
Finalize(imageCropper, outImage);
};
cropViewController.OnDidFinishCancelled = (cancelled) =>
{
imageCropper.Failure?.Invoke();
UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null);
};
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(cropViewController, true, null);
if(imageCropper.PageTitle != null && imageCropper.PageTitle.Length > 0)
{
cropViewController.Title = imageCropper.PageTitle;
}
}
private static async void Finalize(ImageCropper imageCropper, UIImage image)
{
string documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string jpgFilename = System.IO.Path.Combine(documentsDirectory, Guid.NewGuid().ToString() + ".jpg");
NSData imgData = image.AsJPEG();
NSError err;
// small delay
await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(100));
if (imgData.Save(jpgFilename, false, out err))
{
imageCropper.Success?.Invoke(jpgFilename);
}
else
{
Debug.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription);
imageCropper.Failure?.Invoke();
}
UIApplication.SharedApplication.KeyWindow.RootViewController.DismissViewController(true, null);
}
}
}