-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathimagepicker.component.ts
More file actions
68 lines (58 loc) · 1.75 KB
/
imagepicker.component.ts
File metadata and controls
68 lines (58 loc) · 1.75 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
import { Component, NgZone } from '@angular/core';
import { ImageAsset, ImageSource } from '@nativescript/core';
import { ImagePicker, create, ImagePickerSelection } from '@nativescript/imagepicker';
@Component({
selector: 'demo-imagepicker',
templateUrl: 'imagepicker.component.html',
})
export class ImagepickerComponent {
imageAssets: ImagePickerSelection[] = [];
imageSrc: ImageAsset | ImageSource;
isSingleMode: boolean = true;
thumbSize: number = 80;
previewSize: number = 300;
constructor(private _ngZone: NgZone) {}
public onSelectMultipleTap() {
this.isSingleMode = false;
let context = create({
mode: 'multiple',
});
this.startSelection(context);
}
public onSelectSingleTap() {
this.isSingleMode = true;
let context = create({
mode: 'single',
});
this.startSelection(context);
}
private startSelection(context: ImagePicker) {
context
.authorize()
.then((authResult) => {
this._ngZone.run(() => {
this.imageAssets = [];
this.imageSrc = null;
});
if (authResult.authorized) {
return context.present().then((selection) => {
this._ngZone.run(() => {
console.log('Selection done: ' + JSON.stringify(selection));
this.imageSrc = this.isSingleMode && selection.length > 0 ? selection[0].asset : null;
// set the images to be loaded from the assets with optimal sizes (optimize memory usage)
selection.forEach((el) => {
el.asset.options.width = this.isSingleMode ? this.previewSize : this.thumbSize;
el.asset.options.height = this.isSingleMode ? this.previewSize : this.thumbSize;
});
this.imageAssets = selection;
});
});
} else {
console.log('Unauthorised');
}
})
.catch(function (e) {
console.log(e);
});
}
}