diff --git a/apps/tests/image-source-tests.ts b/apps/tests/image-source-tests.ts index cf7cbfd4e..4c0630d46 100644 --- a/apps/tests/image-source-tests.ts +++ b/apps/tests/image-source-tests.ts @@ -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); +} diff --git a/image-source/image-source-common.ts b/image-source/image-source-common.ts index 06f7f6028..4b4b1c49a 100644 --- a/image-source/image-source-common.ts +++ b/image-source/image-source-common.ts @@ -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; diff --git a/image-source/image-source.android.ts b/image-source/image-source.android.ts index 5c85f73f2..ca871a906 100644 --- a/image-source/image-source.android.ts +++ b/image-source/image-source.android.ts @@ -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; diff --git a/image-source/image-source.d.ts b/image-source/image-source.d.ts index 9d78e6451..2e1a3ee16 100644 --- a/image-source/image-source.d.ts +++ b/image-source/image-source.d.ts @@ -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. @@ -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. diff --git a/image-source/image-source.ios.ts b/image-source/image-source.ios.ts index 3b191f6e8..548a4dc22 100644 --- a/image-source/image-source.ios.ts +++ b/image-source/image-source.ios.ts @@ -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; }