forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.ios.ts
More file actions
60 lines (48 loc) · 2.54 KB
/
camera.ios.ts
File metadata and controls
60 lines (48 loc) · 2.54 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
import imageSource = require("image-source");
import frame = require("ui/frame");
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
public static ObjCProtocols = [UIImagePickerControllerDelegate];
static new(): UIImagePickerControllerDelegateImpl {
return <UIImagePickerControllerDelegateImpl>super.new();
}
private _callback: (result?: imageSource.ImageSource) => void;
public initWithCallback(callback: (result?: imageSource.ImageSource) => void): UIImagePickerControllerDelegateImpl {
this._callback = callback;
return this;
}
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
if (info) {
var source = info.valueForKey(UIImagePickerControllerOriginalImage);
if (source) {
var image = imageSource.fromNativeSource(source);
if (this._callback) {
this._callback(image);
}
}
}
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
}
imagePickerControllerDidCancel(picker): void {
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
}
}
export var takePicture = function (): Promise<imageSource.ImageSource> {
return new Promise<imageSource.ImageSource>((resolve, reject) => {
var imagePickerController = new UIImagePickerController();
var listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve);
imagePickerController.delegate = listener;
if (UIDevice.currentDevice().model !== "iPhone Simulator") {
// UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera is not available in emulators!
imagePickerController.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera);
imagePickerController.sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
}
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.UIModalPresentationCurrentContext;
var topMostFrame = frame.topmost();
if (topMostFrame) {
var viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
if (viewController) {
viewController.presentModalViewControllerAnimated(imagePickerController, true);
}
}
});
}