Skip to content

Commit ed98e53

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#149 from NativeScript/image-source-from-base64
load image-source from base64 string added + tests
2 parents f0abe66 + 97cfeb5 commit ed98e53

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

apps/tests/image-source-tests.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,35 @@ export function testBase64Encode_JPEG() {
159159
expected,
160160
"Base 64 encoded JPEG");
161161
}
162+
163+
export function testLoadFromBase64Encode_JPEG() {
164+
var img: imageSource.ImageSource;
165+
if (app.android) {
166+
img = imageSource.fromBase64(expectedAndoirdJpeg);
167+
} else if (app.ios) {
168+
if (platform.device.osVersion[0] === '7') {
169+
img = imageSource.fromBase64(expectedIos7Jpeg);
170+
}
171+
else {
172+
img = imageSource.fromBase64(expectedIos8Jpeg);
173+
}
174+
}
175+
176+
TKUnit.assert(img !== null, "Actual: " + img);
177+
}
178+
179+
export function testLoadFromBase64Encode_PNG() {
180+
var img: imageSource.ImageSource;
181+
if (app.android) {
182+
img = imageSource.fromBase64(expectedAndroidPng);
183+
} else if (app.ios) {
184+
if (platform.device.osVersion[0] === '7') {
185+
img = imageSource.fromBase64(expectedIos7Png);
186+
}
187+
else {
188+
img = imageSource.fromBase64(expectedIos8Png);
189+
}
190+
}
191+
192+
TKUnit.assert(img !== null, "Actual: " + img);
193+
}

image-source/image-source-common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export function fromData(data: any): definition.ImageSource {
2121
return image.loadFromData(data) ? image : null;
2222
}
2323

24+
export function fromBase64(source: string): definition.ImageSource {
25+
var image = new definition.ImageSource();
26+
return image.loadFromBase64(source) ? image : null;
27+
}
28+
2429
export function fromNativeSource(source: any): definition.ImageSource {
2530
var image = new definition.ImageSource();
2631
return image.setNativeSource(source) ? image : null;

image-source/image-source.android.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ export class ImageSource implements definition.ImageSource {
4747
return this.android != null;
4848
}
4949

50+
public loadFromBase64(source: string): boolean {
51+
if (types.isString(source)) {
52+
var bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT);
53+
this.android = android.graphics.BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
54+
}
55+
return this.android != null;
56+
}
57+
5058
public setNativeSource(source: any): boolean {
5159
this.android = source;
5260
return source != null;

image-source/image-source.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ declare module "image-source" {
4545
*/
4646
loadFromData(data: any): boolean;
4747

48+
/**
49+
* Loads this instance from the specified native image data.
50+
* @param source The Base64 string to load the image from.
51+
*/
52+
loadFromBase64(source: string): boolean;
53+
4854
/**
4955
* Sets the provided native source object (typically a Bitmap).
5056
* This will update either the android or ios properties, depending on the target os.
@@ -86,6 +92,12 @@ declare module "image-source" {
8692
*/
8793
export function fromData(data: any): ImageSource;
8894

95+
/**
96+
* Creates a new ImageSource instance and loads it from the specified resource name.
97+
* @param source The Base64 string to load the image from.
98+
*/
99+
export function fromBase64(source: string): ImageSource;
100+
89101
/**
90102
* Creates a new ImageSource instance and sets the provided native source object (typically a Bitmap).
91103
* The native source object will update either the android or ios properties, depending on the target os.

image-source/image-source.ios.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ export class ImageSource implements definition.ImageSource {
2929
}
3030

3131
public loadFromData(data: any): boolean {
32-
this.ios = UIImage.imageWithData(data);
32+
this.ios = UIImage.imageWithData(data);
33+
return this.ios != null;
34+
}
35+
36+
public loadFromBase64(source: string): boolean {
37+
if (types.isString(source)) {
38+
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
39+
this.ios = UIImage.imageWithData(data);
40+
}
3341
return this.ios != null;
3442
}
3543

0 commit comments

Comments
 (0)