Merge pull request #149 from NativeScript/image-source-from-base64

load image-source from base64 string added + tests
This commit is contained in:
Vladimir Enchev
2015-05-12 14:51:19 +03:00
5 changed files with 66 additions and 1 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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.

View File

@@ -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;
}