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
81 changes: 42 additions & 39 deletions apps/tests/ui/image/image-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ export var test_settingImageSource = function () {
}
*/

export var test_SettingImageSrc = function (done) {
// >> img-create-src
var image = new ImageModule.Image();
image.src = "https://www.google.com/images/errors/logo_sm_2.png";
// << img-create-src

function runImageTest(done, image: ImageModule.Image, src: string) {
image.src = null;

var testModel = new ObservableModule.Observable();
Expand All @@ -61,10 +56,16 @@ export var test_SettingImageSrc = function (done) {
TKUnit.assertTrue(!image.isLoading, "Image.isLoading should be false.");
TKUnit.assertTrue(!testModel.get("imageIsLoading"), "imageIsLoading on viewModel should be false.");
TKUnit.assertTrue(imageIsLoaded, "imageIsLoading should be true.");
done(null);
if (done) {
done(null);
}
}
catch (e) {
done(e);
if (done) {
done(e);
} else {
throw e;
}
}
};

Expand All @@ -74,53 +75,55 @@ export var test_SettingImageSrc = function (done) {
twoWay: true
}, testModel);

image.src = "https://www.google.com/images/errors/logo_sm_2.png";
image.src = src;
testModel.on(ObservableModule.Observable.propertyChangeEvent, handler);
TKUnit.assertTrue(image.isLoading, "Image.isLoading should be true.");
TKUnit.assertTrue(testModel.get("imageIsLoading"), "model.isLoading should be true.");
if (done) {
TKUnit.assertTrue(image.isLoading, "Image.isLoading should be true.");
TKUnit.assertTrue(testModel.get("imageIsLoading"), "model.isLoading should be true.");
} else {
// Since it is synchronous check immediately.
handler(null);
}
}

export var test_SettingImageSrcToFileWithinApp = function (done) {
export var test_SettingImageSrc = function (done) {
// >> img-create-src
var image = new ImageModule.Image();
image.src = "https://www.google.com/images/errors/logo_sm_2.png";
// << img-create-src
runImageTest(done, image, image.src)
}

export var test_SettingImageSrcToFileWithinApp = function () {
// >> img-create-local
var image = new ImageModule.Image();
image.src = "~/logo.png";
// << img-create-local

var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image> views[0];
TKUnit.waitUntilReady(() => !testImage.isLoading, 3);
try {
TKUnit.assertTrue(!testImage.isLoading, "isLoading should be false.");
done(null);
}
catch (e) {
done(e);
}
}

helper.buildUIAndRunTest(image, testFunc);
runImageTest(null, image, image.src)
}

export var test_SettingImageSrcToDataURI = function (done) {
export var test_SettingImageSrcToDataURI = function () {
// >> img-create-datauri
var image = new ImageModule.Image();
image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAACAAAAKAAAAAIAAAACAAAARiS4uJEAAAASSURBVBgZYvjPwABHSMz/DAAAAAD//0GWpK0AAAAOSURBVGNgYPiPhBgQAACEvQv1D5y/pAAAAABJRU5ErkJggg==";
// << img-create-datauri

var testFunc = function (views: Array<ViewModule.View>) {
var testImage = <ImageModule.Image>views[0];
TKUnit.waitUntilReady(() => !testImage.isLoading, 3);
try {
TKUnit.assertTrue(!testImage.isLoading, "isLoading should be false.");
TKUnit.assertNotNull(testImage.imageSource);
done(null);
}
catch (e) {
done(e);
}
}
runImageTest(null, image, image.src)
}

helper.buildUIAndRunTest(image, testFunc);
export var test_SettingImageSrcToFileWithinAppAsync = function (done) {
var image = new ImageModule.Image();
image.loadMode = "async";
image.src = "~/logo.png";
runImageTest(done, image, image.src)
}

export var test_SettingImageSrcToDataURIAsync = function (done) {
var image = new ImageModule.Image();
image.loadMode = "async";
image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAACAAAAKAAAAAIAAAACAAAARiS4uJEAAAASSURBVBgZYvjPwABHSMz/DAAAAAD//0GWpK0AAAAOSURBVGNgYPiPhBgQAACEvQv1D5y/pAAAAABJRU5ErkJggg==";
runImageTest(done, image, image.src)
}

export var test_SettingStretch_AspectFit = function () {
Expand Down
29 changes: 8 additions & 21 deletions http/http-request.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
},
toImage: () => {
ensureImageSource();
if (UIImage.imageWithData["async"]) {
return UIImage.imageWithData["async"](UIImage, [data])
.then(image => {
if (!image) {
throw new Error("Response content may not be converted to an Image");
}

var source = new imageSource.ImageSource();
source.setNativeSource(image);
return source;
});
}

return new Promise<any>((resolveImage, rejectImage) => {
var img = imageSource.fromData(data);
if (img instanceof imageSource.ImageSource) {
resolveImage(img);
} else {
rejectImage(new Error("Response content may not be converted to an Image"));
}

return new Promise((resolve, reject) => {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
if (image) {
resolve(imageSource.fromNativeSource(image))
} else {
reject(new Error("Response content may not be converted to an Image"));
}
});
});
},
toFile: (destinationFilePath?: string) => {
Expand Down
9 changes: 3 additions & 6 deletions http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ export function getJSON<T>(arg: any): Promise<T> {
}

export function getImage(arg: any): Promise<image.ImageSource> {
return new Promise<image.ImageSource>((resolve, reject) => {
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => {
r.content.toImage().then(source => resolve(source), e => reject(e));
}, e => reject(e));
});
return httpRequest
.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(responce => responce.content.toImage());
}

export function getFile(arg: any, destinationFilePath?: string): Promise<any> {
Expand Down
26 changes: 25 additions & 1 deletion image-source/image-source.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ export class ImageSource implements definition.ImageSource {
// Load BitmapDrawable with getDrawable to make use of Android internal caching
var bitmapDrawable = <android.graphics.drawable.BitmapDrawable>res.getDrawable(identifier);
if (bitmapDrawable && bitmapDrawable.getBitmap) {
this.android = bitmapDrawable.getBitmap();
this.android = bitmapDrawable.getBitmap();
}
}
}

return this.android != null;
}

public fromResource(name: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.loadFromResource(name));
});
}

public loadFromFile(path: string): boolean {
ensureFS();

Expand All @@ -64,11 +70,23 @@ export class ImageSource implements definition.ImageSource {
return this.android != null;
}

public fromFile(path: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.loadFromFile(path));
});
}

public loadFromData(data: any): boolean {
this.android = android.graphics.BitmapFactory.decodeStream(data);
return this.android != null;
}

public fromData(data: any): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.loadFromData(data));
});
}

public loadFromBase64(source: string): boolean {
if (types.isString(source)) {
var bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT);
Expand All @@ -77,6 +95,12 @@ export class ImageSource implements definition.ImageSource {
return this.android != null;
}

public fromBase64(data: any): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.loadFromBase64(data));
});
}

public setNativeSource(source: any): boolean {
this.android = source;
return source != null;
Expand Down
24 changes: 24 additions & 0 deletions image-source/image-source.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,47 @@ declare module "image-source" {
*/
loadFromResource(name: string): boolean;

/**
* Loads this instance from the specified resource name asynchronously.
* @param name The name of the resource (without its extension).
*/
fromResource(name: string): Promise<boolean>;

/**
* Loads this instance from the specified file.
* @param path The location of the file on the file system.
*/
loadFromFile(path: string): boolean;

/**
* Loads this instance from the specified file asynchronously.
* @param path The location of the file on the file system.
*/
fromFile(path: string): Promise<boolean>;

/**
* Loads this instance from the specified native image data.
* @param data The native data (byte array) to load the image from. This will be either Stream for Android or NSData for iOS.
*/
loadFromData(data: any): boolean;

/**
* Loads this instance from the specified native image data asynchronously.
* @param data The native data (byte array) to load the image from. This will be either Stream for Android or NSData for iOS.
*/
fromData(data: any): Promise<boolean>;

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

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

/**
* Sets the provided native source object (typically a Bitmap).
Expand Down
72 changes: 70 additions & 2 deletions image-source/image-source.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,30 @@ export class ImageSource implements definition.ImageSource {
public ios: UIImage;

public loadFromResource(name: string): boolean {
this.ios = UIImage.imageNamed(name) || UIImage.imageNamed(`${name}.jpg`);
this.ios = (<any>UIImage).tns_safeImageNamed(name) || (<any>UIImage).tns_safeImageNamed(`${name}.jpg`);
return this.ios != null;
}

public fromResource(name: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
(<any>UIImage).tns_safeDecodeImageNamedCompletion(name, image => {
if (image) {
this.ios = image;
resolve(true);
} else {
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, image => {
this.ios = image;
resolve(true);
});
}
});
} catch (ex) {
reject(ex);
}
});
}

public loadFromFile(path: string): boolean {
var fileName = types.isString(path) ? path.trim() : "";

Expand All @@ -26,19 +46,67 @@ export class ImageSource implements definition.ImageSource {
return this.ios != null;
}

public fromFile(path: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
var fileName = types.isString(path) ? path.trim() : "";

if (fileName.indexOf("~/") === 0) {
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
}

(<any>UIImage).tns_decodeImageWidthContentsOfFileCompletion(fileName, image => {
this.ios = image;
resolve(true);
});
} catch (ex) {
reject(ex);
}
});
}

public loadFromData(data: any): boolean {
this.ios = UIImage.imageWithData(data);
return this.ios != null;
}

public fromData(data: any): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
this.ios = image;
resolve(true);
});
} catch (ex) {
reject(ex);
}
});
}

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 fromBase64(source: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
UIImage.imageWithData["async"](UIImage, [data]).then(image => {
this.ios = image;
resolve(true);
});

} catch (ex) {
reject(ex);
}
});
}

public setNativeSource(source: any): boolean {
this.ios = source;
return source != null;
Expand Down Expand Up @@ -103,4 +171,4 @@ function getImageData(instance: UIImage, format: string, quality = 1.0): NSData

}
return data;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"typings": "tns-core-modules.d.ts",
"dependencies": {
"tns-core-modules-widgets": "2.0.0"
"tns-core-modules-widgets": "next"
},
"nativescript": {
"platforms": {
Expand Down
Loading