This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathMediaPicker.ios.cs
171 lines (132 loc) · 6.22 KB
/
MediaPicker.ios.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Linq;
using System.Threading.Tasks;
using Foundation;
using MobileCoreServices;
using Photos;
using UIKit;
namespace Xamarin.Essentials
{
public static partial class MediaPicker
{
static UIImagePickerController picker;
static bool PlatformIsCaptureSupported
=> UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera);
static Task<FileResult> PlatformPickPhotoAsync(MediaPickerOptions options)
=> PhotoAsync(options, true, true);
static Task<FileResult> PlatformCapturePhotoAsync(MediaPickerOptions options)
=> PhotoAsync(options, true, false);
static Task<FileResult> PlatformPickVideoAsync(MediaPickerOptions options)
=> PhotoAsync(options, false, true);
static Task<FileResult> PlatformCaptureVideoAsync(MediaPickerOptions options)
=> PhotoAsync(options, false, false);
static async Task<FileResult> PhotoAsync(MediaPickerOptions options, bool photo, bool pickExisting)
{
var sourceType = pickExisting ? UIImagePickerControllerSourceType.PhotoLibrary : UIImagePickerControllerSourceType.Camera;
var mediaType = photo ? UTType.Image : UTType.Movie;
if (!UIImagePickerController.IsSourceTypeAvailable(sourceType))
throw new FeatureNotSupportedException();
if (!UIImagePickerController.AvailableMediaTypes(sourceType).Contains(mediaType))
throw new FeatureNotSupportedException();
// microphone only needed if video will be captured
if (!photo && !pickExisting)
await Permissions.EnsureGrantedAsync<Permissions.Microphone>();
// Check if picking existing or not and ensure permission accordingly as they can be set independently from each other
if (pickExisting && !Platform.HasOSVersion(11, 0))
await Permissions.EnsureGrantedAsync<Permissions.Photos>();
if (!pickExisting)
await Permissions.EnsureGrantedAsync<Permissions.Camera>();
var vc = Platform.GetCurrentViewController(true);
picker = new UIImagePickerController();
picker.SourceType = sourceType;
picker.MediaTypes = new string[] { mediaType };
picker.AllowsEditing = false;
if (!photo && !pickExisting)
picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
if (!string.IsNullOrWhiteSpace(options?.Title))
picker.Title = options.Title;
if (DeviceInfo.Idiom == DeviceIdiom.Tablet && picker.PopoverPresentationController != null && vc.View != null)
picker.PopoverPresentationController.SourceRect = vc.View.Bounds;
var tcs = new TaskCompletionSource<FileResult>(picker);
picker.Delegate = new PhotoPickerDelegate
{
CompletedHandler = async info =>
{
GetFileResult(info, tcs);
await vc.DismissViewControllerAsync(true);
}
};
if (picker.PresentationController != null)
{
picker.PresentationController.Delegate = new Platform.UIPresentationControllerDelegate
{
DismissHandler = () => GetFileResult(null, tcs)
};
}
await vc.PresentViewControllerAsync(picker, true);
var result = await tcs.Task;
picker?.Dispose();
picker = null;
return result;
}
static void GetFileResult(NSDictionary info, TaskCompletionSource<FileResult> tcs)
{
try
{
tcs.TrySetResult(DictionaryToMediaFile(info));
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
}
static FileResult DictionaryToMediaFile(NSDictionary info)
{
if (info == null)
return null;
PHAsset phAsset = null;
NSUrl assetUrl = null;
if (Platform.HasOSVersion(11, 0))
{
assetUrl = info[UIImagePickerController.ImageUrl] as NSUrl;
// Try the MediaURL sometimes used for videos
if (assetUrl == null)
assetUrl = info[UIImagePickerController.MediaURL] as NSUrl;
if (assetUrl != null)
{
if (!assetUrl.Scheme.Equals("assets-library", StringComparison.InvariantCultureIgnoreCase))
return new UIDocumentFileResult(assetUrl);
phAsset = info.ValueForKey(UIImagePickerController.PHAsset) as PHAsset;
}
}
if (phAsset == null)
{
assetUrl = info[UIImagePickerController.ReferenceUrl] as NSUrl;
if (assetUrl != null)
phAsset = PHAsset.FetchAssets(new NSUrl[] { assetUrl }, null)?.LastObject as PHAsset;
}
if (phAsset == null || assetUrl == null)
{
var img = info.ValueForKey(UIImagePickerController.OriginalImage) as UIImage;
if (img != null)
return new UIImageFileResult(img);
}
if (phAsset == null || assetUrl == null)
return null;
string originalFilename;
if (Platform.HasOSVersion(9, 0))
originalFilename = PHAssetResource.GetAssetResources(phAsset).FirstOrDefault()?.OriginalFilename;
else
originalFilename = phAsset.ValueForKey(new NSString("filename")) as NSString;
return new PHAssetFileResult(assetUrl, phAsset, originalFilename);
}
class PhotoPickerDelegate : UIImagePickerControllerDelegate
{
public Action<NSDictionary> CompletedHandler { get; set; }
public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info) =>
CompletedHandler?.Invoke(info);
public override void Canceled(UIImagePickerController picker) =>
CompletedHandler?.Invoke(null);
}
}
}