forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-source.ios.ts
More file actions
108 lines (84 loc) · 2.83 KB
/
image-source.ios.ts
File metadata and controls
108 lines (84 loc) · 2.83 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
import definition = require("image-source");
import types = require("utils/types");
import fs = require("file-system");
import common = require("image-source/image-source-common");
import enums = require("ui/enums");
// merge the exports of the common file with the exports of this file
declare var exports;
require("utils/module-merge").merge(common, exports);
export class ImageSource implements definition.ImageSource {
public android: android.graphics.Bitmap;
public ios: UIImage;
public loadFromResource(name: string): boolean {
this.ios = UIImage.imageNamed(name);
return this.ios != null;
}
public loadFromFile(path: string): boolean {
var fileName = types.isString(path) ? path.trim() : "";
if (fileName.indexOf("~/") === 0) {
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
}
this.ios = UIImage.imageWithContentsOfFile(fileName);
return this.ios != null;
}
public loadFromData(data: any): boolean {
this.ios = UIImage.imageWithData(data);
return this.ios != null;
}
public loadFromBase64(source: string): boolean {
if (types.isString(source)) {
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
this.ios = UIImage.imageWithData(data);
}
return this.ios != null;
}
public setNativeSource(source: any): boolean {
this.ios = source;
return source != null;
}
public saveToFile(path: string, format: string, quality?: number): boolean {
if (!this.ios) {
return false;
}
var data = getImageData(this.ios, format, quality);
if (data) {
return data.writeToFileAtomically(path, true);
}
return false;
}
public toBase64String(format: string, quality?: number): string {
var res = null;
if (!this.ios) {
return res;
}
var data = getImageData(this.ios, format, quality);
if (data) {
res = data.base64Encoding();
}
return res;
}
get height(): number {
if (this.ios) {
return this.ios.size.height;
}
return NaN;
}
get width(): number {
if (this.ios) {
return this.ios.size.width;
}
return NaN;
}
}
function getImageData(instance: UIImage, format: string, quality = 1.0): NSData {
var data = null;
switch (format) {
case enums.ImageFormat.png: // PNG
data = UIImagePNGRepresentation(instance);
break;
case enums.ImageFormat.jpeg: // JPEG
data = UIImageJPEGRepresentation(instance, quality);
break;
}
return data;
}