-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
save-image-page.js
76 lines (70 loc) · 2.47 KB
/
save-image-page.js
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
import { ImageAsset, ImageSource, Observable, knownFolders, path } from "@nativescript/core";
export function onNavigatingTo(args) {
const page = args.object;
const vm = new Observable();
vm.set("fps", "0");
const folder = knownFolders.currentApp();
const filePath = path.join(folder.path, "images/logo.png");
vm.set("imagePath", filePath);
page.bindingContext = vm;
}
export function makeCopyFromFile(args) {
const page = args.object.page;
const vm = page.bindingContext;
const folder = knownFolders.currentApp();
const imagePath = path.join(folder.path, "images/logo.png");
// >> image-source-save-from-file
const img = ImageSource.fromFileSync(imagePath);
const folderDest = knownFolders.documents();
const pathDest = path.join(folderDest.path, "test.png");
const saved = img.saveToFile(pathDest, "png");
if (saved) {
console.log("Image saved successfully!");
// >> (hide)
vm.set("imageCopyPath", pathDest);
// << (hide)
}
// << image-source-save-from-file
}
export function makeCopyFromAsset(args) {
const page = args.object.page;
const vm = page.bindingContext;
const folder = knownFolders.currentApp();
const pathImage = path.join(folder.path, "images/logo.png");
const imageAsset = new ImageAsset(pathImage);
imageAsset.options = {
width: 100,
height: 100,
keepAspectRatio: true
};
// >> image-source-save-from-asset
ImageSource.fromAsset(imageAsset)
.then((imageSource) => {
const folder = knownFolders.documents().path;
const fileName = "test.png";
const filePath = path.join(folder, fileName);
const saved = imageSource.saveToFile(filePath, "png");
if (saved) {
console.log("Image saved successfully!");
// >> (hide)
vm.set("imageAssetCopyPath", filePath);
// << (hide)
}
})
.catch((e) => {
console.log("Error: ");
console.log(e);
});
// << image-source-save-from-asset
}
export function makeBase64String(args) {
const page = args.object.page;
const vm = page.bindingContext;
// >> image-source-create-base64
const folder = knownFolders.currentApp();
const filePath = path.join(folder.path, "images/logo.png");
const img = ImageSource.fromFileSync(filePath);
const base64String = img.toBase64String("png");
// << image-source-create-base64
vm.set("base64String", base64String);
}