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
126 lines (107 loc) · 5.19 KB
/
Copy pathcamera.ios.ts
File metadata and controls
126 lines (107 loc) · 5.19 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
import types = require("utils/types");
import * as cameraCommonModule from "./camera-common";
import * as imageSourceModule from "image-source";
import * as frameModule from "ui/frame";
class UIImagePickerControllerDelegateImpl extends NSObject implements UIImagePickerControllerDelegate {
public static ObjCProtocols = [UIImagePickerControllerDelegate];
static new(): UIImagePickerControllerDelegateImpl {
return <UIImagePickerControllerDelegateImpl>super.new();
}
private _callback: (result?) => void;
private _width: number;
private _height: number;
private _keepAspectRatio: boolean;
private _saveToGallery: boolean;
public initWithCallback(callback: (result?) => void): UIImagePickerControllerDelegateImpl {
this._callback = callback;
return this;
}
public initWithCallbackAndOptions(callback: (result?) => void, options?): UIImagePickerControllerDelegateImpl {
this._callback = callback;
if (options) {
this._width = options.width;
this._height = options.height;
this._saveToGallery = options.saveToGallery;
this._keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
}
return this;
}
imagePickerControllerDidFinishPickingMediaWithInfo(picker, info): void {
if (info) {
var source = info.valueForKey(UIImagePickerControllerOriginalImage);
if (source) {
var image = null;
if (this._width || this._height) {
var newSize = null;
if (this._keepAspectRatio) {
var common: typeof cameraCommonModule = require("./camera-common");
var aspectSafeSize = common.getAspectSafeDimensions(source.size.width, source.size.height, this._width, this._height);
newSize = CGSizeMake(aspectSafeSize.width, aspectSafeSize.height);
}
else {
newSize = CGSizeMake(this._width, this._height);
}
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0);
source.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height));
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
var imageSource: typeof imageSourceModule = require("image-source");
var imageSourceResult = image ? imageSource.fromNativeSource(image) : imageSource.fromNativeSource(source);
if (this._callback) {
this._callback(imageSourceResult);
if(this._saveToGallery) {
UIImageWriteToSavedPhotosAlbum(imageSourceResult.ios, null, null, null);
}
}
}
}
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
listener = null;
}
imagePickerControllerDidCancel(picker): void {
picker.presentingViewController.dismissViewControllerAnimatedCompletion(true, null);
listener = null;
}
}
var listener;
export var takePicture = function (options): Promise<any> {
return new Promise((resolve, reject) => {
listener = null;
var imagePickerController = new UIImagePickerController();
var reqWidth = 0;
var reqHeight = 0;
var keepAspectRatio = true;
var saveToGallery = true;
if (options) {
reqWidth = options.width || 0;
reqHeight = options.height || reqWidth;
keepAspectRatio = types.isNullOrUndefined(options.keepAspectRatio) ? true : options.keepAspectRatio;
saveToGallery = options.saveToGallery ? true : false;
}
if (reqWidth && reqHeight) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { width: reqWidth, height: reqHeight, keepAspectRatio: keepAspectRatio, saveToGallery: saveToGallery });
} else if (saveToGallery) {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallbackAndOptions(resolve, { saveToGallery: saveToGallery });
}
else {
listener = UIImagePickerControllerDelegateImpl.new().initWithCallback(resolve);
}
imagePickerController.delegate = listener;
var sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera;
var mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(sourceType);
if (mediaTypes) {
imagePickerController.mediaTypes = mediaTypes;
imagePickerController.sourceType = sourceType;
}
imagePickerController.modalPresentationStyle = UIModalPresentationStyle.UIModalPresentationCurrentContext;
var frame: typeof frameModule = require("ui/frame");
var topMostFrame = frame.topmost();
if (topMostFrame) {
var viewController: UIViewController = topMostFrame.currentPage && topMostFrame.currentPage.ios;
if (viewController) {
viewController.presentViewControllerAnimatedCompletion(imagePickerController, true, null);
}
}
});
}