Skip to content

Commit 674f4e7

Browse files
committed
Merge pull request #2111 from NativeScript/widgets-ios-async-image
Optional async mode for loading images from local files
2 parents 2e86d45 + 1b395aa commit 674f4e7

10 files changed

Lines changed: 266 additions & 108 deletions

File tree

‎apps/tests/ui/image/image-tests.ts‎

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ export var test_settingImageSource = function () {
4242
}
4343
*/
4444

45-
export var test_SettingImageSrc = function (done) {
46-
// >> img-create-src
47-
var image = new ImageModule.Image();
48-
image.src = "https://www.google.com/images/errors/logo_sm_2.png";
49-
// << img-create-src
50-
45+
function runImageTest(done, image: ImageModule.Image, src: string) {
5146
image.src = null;
5247

5348
var testModel = new ObservableModule.Observable();
@@ -61,10 +56,16 @@ export var test_SettingImageSrc = function (done) {
6156
TKUnit.assertTrue(!image.isLoading, "Image.isLoading should be false.");
6257
TKUnit.assertTrue(!testModel.get("imageIsLoading"), "imageIsLoading on viewModel should be false.");
6358
TKUnit.assertTrue(imageIsLoaded, "imageIsLoading should be true.");
64-
done(null);
59+
if (done) {
60+
done(null);
61+
}
6562
}
6663
catch (e) {
67-
done(e);
64+
if (done) {
65+
done(e);
66+
} else {
67+
throw e;
68+
}
6869
}
6970
};
7071

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

77-
image.src = "https://www.google.com/images/errors/logo_sm_2.png";
78+
image.src = src;
7879
testModel.on(ObservableModule.Observable.propertyChangeEvent, handler);
79-
TKUnit.assertTrue(image.isLoading, "Image.isLoading should be true.");
80-
TKUnit.assertTrue(testModel.get("imageIsLoading"), "model.isLoading should be true.");
80+
if (done) {
81+
TKUnit.assertTrue(image.isLoading, "Image.isLoading should be true.");
82+
TKUnit.assertTrue(testModel.get("imageIsLoading"), "model.isLoading should be true.");
83+
} else {
84+
// Since it is synchronous check immediately.
85+
handler(null);
86+
}
8187
}
8288

83-
export var test_SettingImageSrcToFileWithinApp = function (done) {
89+
export var test_SettingImageSrc = function (done) {
90+
// >> img-create-src
91+
var image = new ImageModule.Image();
92+
image.src = "https://www.google.com/images/errors/logo_sm_2.png";
93+
// << img-create-src
94+
runImageTest(done, image, image.src)
95+
}
96+
97+
export var test_SettingImageSrcToFileWithinApp = function () {
8498
// >> img-create-local
8599
var image = new ImageModule.Image();
86100
image.src = "~/logo.png";
87101
// << img-create-local
88102

89-
var testFunc = function (views: Array<ViewModule.View>) {
90-
var testImage = <ImageModule.Image> views[0];
91-
TKUnit.waitUntilReady(() => !testImage.isLoading, 3);
92-
try {
93-
TKUnit.assertTrue(!testImage.isLoading, "isLoading should be false.");
94-
done(null);
95-
}
96-
catch (e) {
97-
done(e);
98-
}
99-
}
100-
101-
helper.buildUIAndRunTest(image, testFunc);
103+
runImageTest(null, image, image.src)
102104
}
103105

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

110-
var testFunc = function (views: Array<ViewModule.View>) {
111-
var testImage = <ImageModule.Image>views[0];
112-
TKUnit.waitUntilReady(() => !testImage.isLoading, 3);
113-
try {
114-
TKUnit.assertTrue(!testImage.isLoading, "isLoading should be false.");
115-
TKUnit.assertNotNull(testImage.imageSource);
116-
done(null);
117-
}
118-
catch (e) {
119-
done(e);
120-
}
121-
}
112+
runImageTest(null, image, image.src)
113+
}
122114

123-
helper.buildUIAndRunTest(image, testFunc);
115+
export var test_SettingImageSrcToFileWithinAppAsync = function (done) {
116+
var image = new ImageModule.Image();
117+
image.loadMode = "async";
118+
image.src = "~/logo.png";
119+
runImageTest(done, image, image.src)
120+
}
121+
122+
export var test_SettingImageSrcToDataURIAsync = function (done) {
123+
var image = new ImageModule.Image();
124+
image.loadMode = "async";
125+
image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAAACAAAAKAAAAAIAAAACAAAARiS4uJEAAAASSURBVBgZYvjPwABHSMz/DAAAAAD//0GWpK0AAAAOSURBVGNgYPiPhBgQAACEvQv1D5y/pAAAAABJRU5ErkJggg==";
126+
runImageTest(done, image, image.src)
124127
}
125128

126129
export var test_SettingStretch_AspectFit = function () {

‎http/http-request.ios.ts‎

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
9898
},
9999
toImage: () => {
100100
ensureImageSource();
101-
if (UIImage.imageWithData["async"]) {
102-
return UIImage.imageWithData["async"](UIImage, [data])
103-
.then(image => {
104-
if (!image) {
105-
throw new Error("Response content may not be converted to an Image");
106-
}
107-
108-
var source = new imageSource.ImageSource();
109-
source.setNativeSource(image);
110-
return source;
111-
});
112-
}
113-
114-
return new Promise<any>((resolveImage, rejectImage) => {
115-
var img = imageSource.fromData(data);
116-
if (img instanceof imageSource.ImageSource) {
117-
resolveImage(img);
118-
} else {
119-
rejectImage(new Error("Response content may not be converted to an Image"));
120-
}
121-
101+
return new Promise((resolve, reject) => {
102+
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
103+
if (image) {
104+
resolve(imageSource.fromNativeSource(image))
105+
} else {
106+
reject(new Error("Response content may not be converted to an Image"));
107+
}
108+
});
122109
});
123110
},
124111
toFile: (destinationFilePath?: string) => {

‎http/http.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ export function getJSON<T>(arg: any): Promise<T> {
3333
}
3434

3535
export function getImage(arg: any): Promise<image.ImageSource> {
36-
return new Promise<image.ImageSource>((resolve, reject) => {
37-
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
38-
.then(r => {
39-
r.content.toImage().then(source => resolve(source), e => reject(e));
40-
}, e => reject(e));
41-
});
36+
return httpRequest
37+
.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
38+
.then(responce => responce.content.toImage());
4239
}
4340

4441
export function getFile(arg: any, destinationFilePath?: string): Promise<any> {

‎image-source/image-source.android.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,20 @@ export class ImageSource implements definition.ImageSource {
4444
// Load BitmapDrawable with getDrawable to make use of Android internal caching
4545
var bitmapDrawable = <android.graphics.drawable.BitmapDrawable>res.getDrawable(identifier);
4646
if (bitmapDrawable && bitmapDrawable.getBitmap) {
47-
this.android = bitmapDrawable.getBitmap();
47+
this.android = bitmapDrawable.getBitmap();
4848
}
4949
}
5050
}
5151

5252
return this.android != null;
5353
}
5454

55+
public fromResource(name: string): Promise<boolean> {
56+
return new Promise<boolean>((resolve, reject) => {
57+
resolve(this.loadFromResource(name));
58+
});
59+
}
60+
5561
public loadFromFile(path: string): boolean {
5662
ensureFS();
5763

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

73+
public fromFile(path: string): Promise<boolean> {
74+
return new Promise<boolean>((resolve, reject) => {
75+
resolve(this.loadFromFile(path));
76+
});
77+
}
78+
6779
public loadFromData(data: any): boolean {
6880
this.android = android.graphics.BitmapFactory.decodeStream(data);
6981
return this.android != null;
7082
}
7183

84+
public fromData(data: any): Promise<boolean> {
85+
return new Promise<boolean>((resolve, reject) => {
86+
resolve(this.loadFromData(data));
87+
});
88+
}
89+
7290
public loadFromBase64(source: string): boolean {
7391
if (types.isString(source)) {
7492
var bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT);
@@ -77,6 +95,12 @@ export class ImageSource implements definition.ImageSource {
7795
return this.android != null;
7896
}
7997

98+
public fromBase64(data: any): Promise<boolean> {
99+
return new Promise<boolean>((resolve, reject) => {
100+
resolve(this.loadFromBase64(data));
101+
});
102+
}
103+
80104
public setNativeSource(source: any): boolean {
81105
this.android = source;
82106
return source != null;

‎image-source/image-source.d.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,47 @@ declare module "image-source" {
3333
*/
3434
loadFromResource(name: string): boolean;
3535

36+
/**
37+
* Loads this instance from the specified resource name asynchronously.
38+
* @param name The name of the resource (without its extension).
39+
*/
40+
fromResource(name: string): Promise<boolean>;
41+
3642
/**
3743
* Loads this instance from the specified file.
3844
* @param path The location of the file on the file system.
3945
*/
4046
loadFromFile(path: string): boolean;
4147

48+
/**
49+
* Loads this instance from the specified file asynchronously.
50+
* @param path The location of the file on the file system.
51+
*/
52+
fromFile(path: string): Promise<boolean>;
53+
4254
/**
4355
* Loads this instance from the specified native image data.
4456
* @param data The native data (byte array) to load the image from. This will be either Stream for Android or NSData for iOS.
4557
*/
4658
loadFromData(data: any): boolean;
59+
60+
/**
61+
* Loads this instance from the specified native image data asynchronously.
62+
* @param data The native data (byte array) to load the image from. This will be either Stream for Android or NSData for iOS.
63+
*/
64+
fromData(data: any): Promise<boolean>;
4765

4866
/**
4967
* Loads this instance from the specified native image data.
5068
* @param source The Base64 string to load the image from.
5169
*/
5270
loadFromBase64(source: string): boolean;
71+
72+
/**
73+
* Loads this instance from the specified native image data asynchronously.
74+
* @param source The Base64 string to load the image from.
75+
*/
76+
fromBase64(source: string): Promise<boolean>;
5377

5478
/**
5579
* Sets the provided native source object (typically a Bitmap).

‎image-source/image-source.ios.ts‎

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,30 @@ export class ImageSource implements definition.ImageSource {
1111
public ios: UIImage;
1212

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

18+
public fromResource(name: string): Promise<boolean> {
19+
return new Promise<boolean>((resolve, reject) => {
20+
try {
21+
(<any>UIImage).tns_safeDecodeImageNamedCompletion(name, image => {
22+
if (image) {
23+
this.ios = image;
24+
resolve(true);
25+
} else {
26+
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, image => {
27+
this.ios = image;
28+
resolve(true);
29+
});
30+
}
31+
});
32+
} catch (ex) {
33+
reject(ex);
34+
}
35+
});
36+
}
37+
1838
public loadFromFile(path: string): boolean {
1939
var fileName = types.isString(path) ? path.trim() : "";
2040

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

49+
public fromFile(path: string): Promise<boolean> {
50+
return new Promise<boolean>((resolve, reject) => {
51+
try {
52+
var fileName = types.isString(path) ? path.trim() : "";
53+
54+
if (fileName.indexOf("~/") === 0) {
55+
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
56+
}
57+
58+
(<any>UIImage).tns_decodeImageWidthContentsOfFileCompletion(fileName, image => {
59+
this.ios = image;
60+
resolve(true);
61+
});
62+
} catch (ex) {
63+
reject(ex);
64+
}
65+
});
66+
}
67+
2968
public loadFromData(data: any): boolean {
3069
this.ios = UIImage.imageWithData(data);
3170
return this.ios != null;
3271
}
3372

73+
public fromData(data: any): Promise<boolean> {
74+
return new Promise<boolean>((resolve, reject) => {
75+
try {
76+
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
77+
this.ios = image;
78+
resolve(true);
79+
});
80+
} catch (ex) {
81+
reject(ex);
82+
}
83+
});
84+
}
85+
3486
public loadFromBase64(source: string): boolean {
3587
if (types.isString(source)) {
3688
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
3789
this.ios = UIImage.imageWithData(data);
3890
}
91+
3992
return this.ios != null;
4093
}
4194

95+
public fromBase64(source: string): Promise<boolean> {
96+
return new Promise<boolean>((resolve, reject) => {
97+
try {
98+
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
99+
UIImage.imageWithData["async"](UIImage, [data]).then(image => {
100+
this.ios = image;
101+
resolve(true);
102+
});
103+
104+
} catch (ex) {
105+
reject(ex);
106+
}
107+
});
108+
}
109+
42110
public setNativeSource(source: any): boolean {
43111
this.ios = source;
44112
return source != null;
@@ -103,4 +171,4 @@ function getImageData(instance: UIImage, format: string, quality = 1.0): NSData
103171

104172
}
105173
return data;
106-
}
174+
}

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"typings": "tns-core-modules.d.ts",
4848
"dependencies": {
49-
"tns-core-modules-widgets": "2.0.0"
49+
"tns-core-modules-widgets": "next"
5050
},
5151
"nativescript": {
5252
"platforms": {

0 commit comments

Comments
 (0)