Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/tests/image-source-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,35 @@ export function testBase64Encode_JPEG() {
expected,
"Base 64 encoded JPEG");
}

export function testLoadFromBase64Encode_JPEG() {
var img: imageSource.ImageSource;
if (app.android) {
img = imageSource.fromBase64(expectedAndoirdJpeg);
} else if (app.ios) {
if (platform.device.osVersion[0] === '7') {
img = imageSource.fromBase64(expectedIos7Jpeg);
}
else {
img = imageSource.fromBase64(expectedIos8Jpeg);
}
}

TKUnit.assert(img !== null, "Actual: " + img);
}

export function testLoadFromBase64Encode_PNG() {
var img: imageSource.ImageSource;
if (app.android) {
img = imageSource.fromBase64(expectedAndroidPng);
} else if (app.ios) {
if (platform.device.osVersion[0] === '7') {
img = imageSource.fromBase64(expectedIos7Png);
}
else {
img = imageSource.fromBase64(expectedIos8Png);
}
}

TKUnit.assert(img !== null, "Actual: " + img);
}
5 changes: 5 additions & 0 deletions image-source/image-source-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export function fromData(data: any): definition.ImageSource {
return image.loadFromData(data) ? image : null;
}

export function fromBase64(source: string): definition.ImageSource {
var image = new definition.ImageSource();
return image.loadFromBase64(source) ? image : null;
}

export function fromNativeSource(source: any): definition.ImageSource {
var image = new definition.ImageSource();
return image.setNativeSource(source) ? image : null;
Expand Down
8 changes: 8 additions & 0 deletions image-source/image-source.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export class ImageSource implements definition.ImageSource {
return this.android != null;
}

public loadFromBase64(source: string): boolean {
if (types.isString(source)) {
var bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT);
this.android = android.graphics.BitmapFactory.decodeByteArray(bytes, 0, bytes.length)
}
return this.android != null;
}

public setNativeSource(source: any): boolean {
this.android = source;
return source != null;
Expand Down
12 changes: 12 additions & 0 deletions image-source/image-source.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ declare module "image-source" {
*/
loadFromData(data: any): boolean;

/**
* Loads this instance from the specified native image data.
* @param source The Base64 string to load the image from.
*/
loadFromBase64(source: string): boolean;

/**
* Sets the provided native source object (typically a Bitmap).
* This will update either the android or ios properties, depending on the target os.
Expand Down Expand Up @@ -86,6 +92,12 @@ declare module "image-source" {
*/
export function fromData(data: any): ImageSource;

/**
* Creates a new ImageSource instance and loads it from the specified resource name.
* @param source The Base64 string to load the image from.
*/
export function fromBase64(source: string): ImageSource;

/**
* Creates a new ImageSource instance and sets the provided native source object (typically a Bitmap).
* The native source object will update either the android or ios properties, depending on the target os.
Expand Down
10 changes: 9 additions & 1 deletion image-source/image-source.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ export class ImageSource implements definition.ImageSource {
}

public loadFromData(data: any): boolean {
this.ios = UIImage.imageWithData(data);
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;
}

Expand Down