Repository navigation
Expand file tree
/
Copy pathcamera.ios.ts
More file actions
275 lines (248 loc) · 12.4 KB
/
Copy pathcamera.ios.ts
File metadata and controls
275 lines (248 loc) · 12.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import * as types from "tns-core-modules/utils/types";
import * as imageSourceModule from "tns-core-modules/image-source/image-source";
import * as imageAssetModule from "tns-core-modules/image-asset/image-asset";
import * as frameModule from "tns-core-modules/ui/frame/frame";
import * as trace from "tns-core-modules/trace/trace";
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
public static ObjCProtocols = [UIImagePickerControllerDelegate];
static new(): UIImagePickerControllerDelegateImpl {
return <UIImagePickerControllerDelegateImpl>super.new();
}
private _callback: (result?) => void;
private _errorCallback: (result?) => void;
private _width: number;
private _height: number;
private _keepAspectRatio: boolean;
private _saveToGallery: boolean;
private _allowsEditing: boolean;
public initWithCallback(callback: (result?) => void, errorCallback: (result?) => void): UIImagePickerControllerDelegateImpl {
this._callback = callback;
this._errorCallback = errorCallback;
return this;
}
public initWithCallbackAndOptions(callback: (result?) => void, errorCallback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
this._callback = callback;
this._errorCallback = errorCallback;
if (options) {
this._width = options.width;
this._height = options.height;
this._saveToGallery = options.saveToGallery;
this._allowsEditing = options.allowsEditing;
this._keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
}
return this;
}
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
if (info) {
let currentDate: Date = new Date();
let source = info.valueForKey(UIImagePickerControllerOriginalImage);
if (this._allowsEditing) {
source = info.valueForKey(UIImagePickerControllerEditedImage);
}
if (source) {
let imageSource: typeof imageSourceModule = require("image-source");
let imageSourceResult = imageSource.fromNativeSource(source);
if (this._callback) {
let imageAsset: imageAssetModule.ImageAsset;
if (this._saveToGallery) {
PHPhotoLibrary.sharedPhotoLibrary().performChangesCompletionHandler(
() => {
PHAssetChangeRequest.creationRequestForAssetFromImage(imageSourceResult.ios);
},
(success, err) => {
if (success) {
let fetchOptions = PHFetchOptions.alloc().init();
let sortDescriptors = NSArray.arrayWithObject(
NSSortDescriptor.sortDescriptorWithKeyAscending("creationDate", false));
fetchOptions.sortDescriptors = sortDescriptors;
fetchOptions.predicate = NSPredicate.predicateWithFormatArgumentArray(
"mediaType = %d", NSArray.arrayWithObject(PHAssetMediaType.Image));
let fetchResult = PHAsset.fetchAssetsWithOptions(fetchOptions);
if (fetchResult.count > 0) {
// Take last picture
let asset = <PHAsset>fetchResult[0];
const dateDiff = asset.creationDate.valueOf() - currentDate.valueOf();
if (Math.abs(dateDiff) > 1000) {
// Image assets create date is rounded when asset is created.
// Display waring if the asset was created more than 1s before/after the current date.
console.warn("Image asset returned was created more than 1 second ago");
}
imageAsset = new imageAssetModule.ImageAsset(asset);
this.setImageAssetAndCallCallback(imageAsset);
}
} else {
trace.write("An error ocurred while saving image to gallery: " +
err, trace.categories.Error, trace.messageType.error);
}
});
} else {
imageAsset = new imageAssetModule.ImageAsset(imageSourceResult.ios);
this.setImageAssetAndCallCallback(imageAsset);
}
}
}
}
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
listener = null;
}
private setImageAssetAndCallCallback(imageAsset: imageAssetModule.ImageAsset) {
if (this._keepAspectRatio) {
let pictureWidth = imageAsset.nativeImage ? imageAsset.nativeImage.size.width : imageAsset.ios.pixelWidth;
let pictureHeight = imageAsset.nativeImage ? imageAsset.nativeImage.size.height : imageAsset.ios.pixelHeight;
let isPictureLandscape = pictureWidth > pictureHeight;
let areOptionsLandscape = this._width > this._height;
if (isPictureLandscape !== areOptionsLandscape) {
let oldWidth = this._width;
this._width = this._height;
this._height = oldWidth;
}
}
imageAsset.options = {
width: this._width,
height: this._height,
keepAspectRatio: this._keepAspectRatio
};
this._callback(imageAsset);
}
imagePickerControllerDidCancel(picker): void {
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
listener = null;
this._errorCallback(new Error("cancelled"));
}
}
let listener;
export let takePicture = function (options): Promise<any> {
return new Promise((resolve, reject) => {
listener = null;
let imagePickerController = UIImagePickerController.new();
let reqWidth = 0;
let reqHeight = 0;
let keepAspectRatio = true;
let saveToGallery = true;
let allowsEditing = false;
if (options) {
reqWidth = options.width || 0;
reqHeight = options.height || reqWidth;
keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? keepAspectRatio : options.keepAspectRatio;
saveToGallery = types.isNullOrUndefined(options.saveToGallery) ? saveToGallery : options.saveToGallery;
allowsEditing = types.isNullOrUndefined(options.allowsEditing) ? allowsEditing : options.allowsEditing;
}
let authStatus = PHPhotoLibrary.authorizationStatus();
if (authStatus !== PHAuthorizationStatus.Authorized) {
saveToGallery = false;
}
if (reqWidth && reqHeight) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
resolve, reject, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery, allowsEditing: allowsEditing });
} else if (saveToGallery) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(
resolve, reject, { saveToGallery: saveToGallery, keepAspectRatio: keepAspectRatio, allowsEditing: allowsEditing });
} else {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve, reject);
}
imagePickerController.delegate = listener;
let sourceType = UIImagePickerControllerSourceType.Camera;
let mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
let imageMediaType = "public.image";
if (mediaTypes && mediaTypes.containsObject(imageMediaType)) {
let mediaTypesArray = new NSMutableArray<string>({ capacity: 1 });
mediaTypesArray.addObject(imageMediaType);
imagePickerController.mediaTypes = mediaTypesArray;
imagePickerController.sourceType = sourceType;
imagePickerController.cameraDevice = options && options.cameraFacing === "front" ?
UIImagePickerControllerCameraDevice.Front : UIImagePickerControllerCameraDevice.Rear;
imagePickerController.allowsEditing = allowsEditing;
}
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext;
let frame: typeof frameModule = require("tns-core-modules/ui/frame");
let topMostFrame = frame.topmost();
if (topMostFrame) {
let viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
if (viewController) {
while (viewController.parentViewController) {
// find top-most view controler
viewController = viewController.parentViewController;
}
while (viewController.presentedViewController) {
// find last presented modal
viewController = viewController.presentedViewController;
}
viewController.presentViewControllerAnimatedCompletion(imagePickerController, true, null);
}
}
});
};
export let isAvailable = function () {
return UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera);
};
export let requestPermissions = function () {
return new Promise(function (resolve, reject) {
requestPhotosPermissions().then(() => {
requestCameraPermissions().then(resolve, reject);
}, reject);
});
};
export let requestPhotosPermissions = function () {
return new Promise(function (resolve, reject) {
let authStatus = PHPhotoLibrary.authorizationStatus();
switch (authStatus) {
case PHAuthorizationStatus.NotDetermined: {
PHPhotoLibrary.requestAuthorization((auth) => {
if (auth === PHAuthorizationStatus.Authorized) {
if (trace.isEnabled()) {
trace.write("Application can access photo library assets.", trace.categories.Debug);
}
resolve();
} else {
reject();
}
});
break;
}
case PHAuthorizationStatus.Authorized: {
if (trace.isEnabled()) {
trace.write("Application can access photo library assets.", trace.categories.Debug);
}
resolve();
break;
}
case PHAuthorizationStatus.Restricted:
case PHAuthorizationStatus.Denied: {
if (trace.isEnabled()) {
trace.write("Application can not access photo library assets.", trace.categories.Debug);
}
reject();
break;
}
}
});
};
export let requestCameraPermissions = function () {
return new Promise(function (resolve, reject) {
let cameraStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo);
switch (cameraStatus) {
case AVAuthorizationStatus.NotDetermined: {
AVCaptureDevice.requestAccessForMediaTypeCompletionHandler(AVMediaTypeVideo, (granted) => {
if (granted) {
resolve();
} else {
reject();
}
});
break;
}
case AVAuthorizationStatus.Authorized: {
resolve();
break;
}
case AVAuthorizationStatus.Restricted:
case AVAuthorizationStatus.Denied: {
if (trace.isEnabled()) {
trace.write("Application can not access Camera assets.", trace.categories.Debug);
}
reject();
break;
}
}
});
};