Skip to content

Commit 051f60d

Browse files
committed
Fix getImageAsync to correctly parse string dimensions as numbers
1 parent bc887df commit 051f60d

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

packages/core/image-asset/image-asset-common.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,30 @@ export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, req
4747
}
4848

4949
export function getRequestedImageSize(src: { width: number; height: number }, options: ImageAssetOptions): { width: number; height: number } {
50-
let reqWidth = options.width || Math.min(src.width, Screen.mainScreen.widthPixels);
51-
let reqHeight = options.height || Math.min(src.height, Screen.mainScreen.heightPixels);
50+
const optionsCopy = { ...(this.options || {}) };
51+
52+
if (typeof optionsCopy.width === 'string') {
53+
const parsedWidth = parseInt(optionsCopy.width, 10);
54+
if (!isNaN(parsedWidth)) {
55+
optionsCopy.width = parsedWidth;
56+
} else {
57+
console.warn('Invalid width value provided: ', optionsCopy.width);
58+
delete optionsCopy.width;
59+
}
60+
}
61+
62+
if (typeof optionsCopy.height === 'string') {
63+
const parsedHeight = parseInt(optionsCopy.height, 10);
64+
if (!isNaN(parsedHeight)) {
65+
optionsCopy.height = parsedHeight;
66+
} else {
67+
console.warn('Invalid height value provided: ', options.height);
68+
delete optionsCopy.height;
69+
}
70+
}
71+
72+
let reqWidth = optionsCopy.width || Math.min(src.width, Screen.mainScreen.widthPixels);
73+
let reqHeight = optionsCopy.height || Math.min(src.height, Screen.mainScreen.heightPixels);
5274

5375
if (options && options.keepAspectRatio) {
5476
const safeAspectSize = getAspectSafeDimensions(src.width, src.height, reqWidth, reqHeight);

packages/core/image-asset/index.android.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,25 @@ export class ImageAsset extends ImageAssetBase {
2626
}
2727

2828
public getImageAsync(callback: (image, error) => void) {
29+
if (!this._android && !this.nativeImage) {
30+
callback(null, 'Asset cannot be found.');
31+
return;
32+
}
33+
34+
const srcWidth = this.nativeImage ? this.nativeImage.size.width : Screen.mainScreen.widthPixels;
35+
const srcHeight = this.nativeImage ? this.nativeImage.size.height : Screen.mainScreen.heightPixels;
36+
const requestedSize = getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options);
37+
38+
const optionsCopy = {
39+
...this.options,
40+
width: requestedSize.width,
41+
height: requestedSize.height,
42+
};
43+
2944
org.nativescript.widgets.Utils.loadImageAsync(
3045
ad.getApplicationContext(),
3146
this.android,
32-
JSON.stringify(this.options || {}),
47+
JSON.stringify(optionsCopy),
3348
Screen.mainScreen.widthPixels,
3449
Screen.mainScreen.heightPixels,
3550
new org.nativescript.widgets.Utils.AsyncImageCallback({
@@ -39,7 +54,7 @@ export class ImageAsset extends ImageAssetBase {
3954
onError(ex) {
4055
callback(null, ex);
4156
},
42-
})
57+
}),
4358
);
4459
}
4560
}

0 commit comments

Comments
 (0)