From 051f60d7496d40decaa2f0c5b1cc5a242f5ffd21 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 2 May 2025 18:13:34 +0100 Subject: [PATCH 1/8] Fix getImageAsync to correctly parse string dimensions as numbers --- .../core/image-asset/image-asset-common.ts | 26 +++++++++++++++++-- packages/core/image-asset/index.android.ts | 19 ++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 798727dd3e..8a2a41415a 100644 --- a/packages/core/image-asset/image-asset-common.ts +++ b/packages/core/image-asset/image-asset-common.ts @@ -47,8 +47,30 @@ export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, req } export function getRequestedImageSize(src: { width: number; height: number }, options: ImageAssetOptions): { width: number; height: number } { - let reqWidth = options.width || Math.min(src.width, Screen.mainScreen.widthPixels); - let reqHeight = options.height || Math.min(src.height, Screen.mainScreen.heightPixels); + const optionsCopy = { ...(this.options || {}) }; + + if (typeof optionsCopy.width === 'string') { + const parsedWidth = parseInt(optionsCopy.width, 10); + if (!isNaN(parsedWidth)) { + optionsCopy.width = parsedWidth; + } else { + console.warn('Invalid width value provided: ', optionsCopy.width); + delete optionsCopy.width; + } + } + + if (typeof optionsCopy.height === 'string') { + const parsedHeight = parseInt(optionsCopy.height, 10); + if (!isNaN(parsedHeight)) { + optionsCopy.height = parsedHeight; + } else { + console.warn('Invalid height value provided: ', options.height); + delete optionsCopy.height; + } + } + + let reqWidth = optionsCopy.width || Math.min(src.width, Screen.mainScreen.widthPixels); + let reqHeight = optionsCopy.height || Math.min(src.height, Screen.mainScreen.heightPixels); if (options && options.keepAspectRatio) { const safeAspectSize = getAspectSafeDimensions(src.width, src.height, reqWidth, reqHeight); diff --git a/packages/core/image-asset/index.android.ts b/packages/core/image-asset/index.android.ts index 055caeff60..785273be51 100644 --- a/packages/core/image-asset/index.android.ts +++ b/packages/core/image-asset/index.android.ts @@ -26,10 +26,25 @@ export class ImageAsset extends ImageAssetBase { } public getImageAsync(callback: (image, error) => void) { + if (!this._android && !this.nativeImage) { + callback(null, 'Asset cannot be found.'); + return; + } + + const srcWidth = this.nativeImage ? this.nativeImage.size.width : Screen.mainScreen.widthPixels; + const srcHeight = this.nativeImage ? this.nativeImage.size.height : Screen.mainScreen.heightPixels; + const requestedSize = getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options); + + const optionsCopy = { + ...this.options, + width: requestedSize.width, + height: requestedSize.height, + }; + org.nativescript.widgets.Utils.loadImageAsync( ad.getApplicationContext(), this.android, - JSON.stringify(this.options || {}), + JSON.stringify(optionsCopy), Screen.mainScreen.widthPixels, Screen.mainScreen.heightPixels, new org.nativescript.widgets.Utils.AsyncImageCallback({ @@ -39,7 +54,7 @@ export class ImageAsset extends ImageAssetBase { onError(ex) { callback(null, ex); }, - }) + }), ); } } From c9583f9347583a2391cb9f5802ffe583cac90198 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 2 May 2025 18:54:49 +0100 Subject: [PATCH 2/8] Use Trace.write for warnings to align with tracing system --- packages/core/image-asset/image-asset-common.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 8a2a41415a..7d350e8f09 100644 --- a/packages/core/image-asset/image-asset-common.ts +++ b/packages/core/image-asset/image-asset-common.ts @@ -1,6 +1,7 @@ import { ImageAsset as ImageAssetDefinition, ImageAssetOptions } from '.'; import { Observable } from '../data/observable'; import { Screen } from '../platform'; +import { Trace } from '../trace/index'; export class ImageAssetBase extends Observable implements ImageAssetDefinition { private _options: ImageAssetOptions; @@ -54,7 +55,7 @@ export function getRequestedImageSize(src: { width: number; height: number }, op if (!isNaN(parsedWidth)) { optionsCopy.width = parsedWidth; } else { - console.warn('Invalid width value provided: ', optionsCopy.width); + Trace.write('Invalid width value provided: ' + optionsCopy.width, Trace.categories.Debug, Trace.messageType.warn); delete optionsCopy.width; } } @@ -64,7 +65,7 @@ export function getRequestedImageSize(src: { width: number; height: number }, op if (!isNaN(parsedHeight)) { optionsCopy.height = parsedHeight; } else { - console.warn('Invalid height value provided: ', options.height); + Trace.write('Invalid height value provided: ' + optionsCopy.height, Trace.categories.Debug, Trace.messageType.warn); delete optionsCopy.height; } } From 2314daa90be255b08cce3914c835ec3948e92908 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 30 May 2025 19:12:13 +0100 Subject: [PATCH 3/8] Use console.warn instead of Trace --- packages/core/image-asset/image-asset-common.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 7d350e8f09..122af7acc1 100644 --- a/packages/core/image-asset/image-asset-common.ts +++ b/packages/core/image-asset/image-asset-common.ts @@ -1,7 +1,6 @@ import { ImageAsset as ImageAssetDefinition, ImageAssetOptions } from '.'; import { Observable } from '../data/observable'; import { Screen } from '../platform'; -import { Trace } from '../trace/index'; export class ImageAssetBase extends Observable implements ImageAssetDefinition { private _options: ImageAssetOptions; @@ -55,7 +54,7 @@ export function getRequestedImageSize(src: { width: number; height: number }, op if (!isNaN(parsedWidth)) { optionsCopy.width = parsedWidth; } else { - Trace.write('Invalid width value provided: ' + optionsCopy.width, Trace.categories.Debug, Trace.messageType.warn); + console.warn('Invalid width value provided: ', optionsCopy.width); delete optionsCopy.width; } } @@ -65,7 +64,7 @@ export function getRequestedImageSize(src: { width: number; height: number }, op if (!isNaN(parsedHeight)) { optionsCopy.height = parsedHeight; } else { - Trace.write('Invalid height value provided: ' + optionsCopy.height, Trace.categories.Debug, Trace.messageType.warn); + console.warn('Invalid height value provided: ', optionsCopy.height); delete optionsCopy.height; } } From de86930dde741efad5db6d06008a20f531909786 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 30 May 2025 19:14:15 +0100 Subject: [PATCH 4/8] ImageAssetOptions width and height cannow be number or string --- packages/core/image-asset/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/image-asset/index.d.ts b/packages/core/image-asset/index.d.ts index c6a7d247d4..dc41012fe3 100644 --- a/packages/core/image-asset/index.d.ts +++ b/packages/core/image-asset/index.d.ts @@ -10,8 +10,8 @@ export class ImageAsset extends Observable { } export interface ImageAssetOptions { - width?: number; - height?: number; + width?: number | string; + height?: number | string; keepAspectRatio?: boolean; autoScaleFactor?: boolean; } From 82bc403eb64f871c486601e18d50b0d508f1d348 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 30 May 2025 19:14:54 +0100 Subject: [PATCH 5/8] Use the getter instead of direct access --- packages/core/image-asset/index.android.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/image-asset/index.android.ts b/packages/core/image-asset/index.android.ts index 785273be51..7f602365b1 100644 --- a/packages/core/image-asset/index.android.ts +++ b/packages/core/image-asset/index.android.ts @@ -26,7 +26,7 @@ export class ImageAsset extends ImageAssetBase { } public getImageAsync(callback: (image, error) => void) { - if (!this._android && !this.nativeImage) { + if (!this.android && !this.nativeImage) { callback(null, 'Asset cannot be found.'); return; } From 14629ab46bb85f5178230a1a81c54198dd4fcc40 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 30 May 2025 19:39:36 +0100 Subject: [PATCH 6/8] fix: used options argument instead of this.options --- packages/core/image-asset/image-asset-common.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 122af7acc1..07555fca8d 100644 --- a/packages/core/image-asset/image-asset-common.ts +++ b/packages/core/image-asset/image-asset-common.ts @@ -47,7 +47,7 @@ export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, req } export function getRequestedImageSize(src: { width: number; height: number }, options: ImageAssetOptions): { width: number; height: number } { - const optionsCopy = { ...(this.options || {}) }; + const optionsCopy = { ...(options || {}) }; if (typeof optionsCopy.width === 'string') { const parsedWidth = parseInt(optionsCopy.width, 10); @@ -69,8 +69,8 @@ export function getRequestedImageSize(src: { width: number; height: number }, op } } - let reqWidth = optionsCopy.width || Math.min(src.width, Screen.mainScreen.widthPixels); - let reqHeight = optionsCopy.height || Math.min(src.height, Screen.mainScreen.heightPixels); + let reqWidth = (optionsCopy.width as number) || Math.min(src.width, Screen.mainScreen.widthPixels); + let reqHeight = (optionsCopy.height as number) || Math.min(src.height, Screen.mainScreen.heightPixels); if (options && options.keepAspectRatio) { const safeAspectSize = getAspectSafeDimensions(src.width, src.height, reqWidth, reqHeight); From ab734b6158ce979f7fd85439d37116ee060b7256 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 30 May 2025 19:48:19 +0100 Subject: [PATCH 7/8] Use the getWidth and getHeight to get the image dimensions --- packages/core/image-asset/index.android.ts | 10 ++++++++-- packages/core/image-asset/index.ios.ts | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/core/image-asset/index.android.ts b/packages/core/image-asset/index.android.ts index 7f602365b1..4e4888597d 100644 --- a/packages/core/image-asset/index.android.ts +++ b/packages/core/image-asset/index.android.ts @@ -31,8 +31,14 @@ export class ImageAsset extends ImageAssetBase { return; } - const srcWidth = this.nativeImage ? this.nativeImage.size.width : Screen.mainScreen.widthPixels; - const srcHeight = this.nativeImage ? this.nativeImage.size.height : Screen.mainScreen.heightPixels; + const srcWidth = this.nativeImage ? ( + typeof this.nativeImage.getWidth === 'function' ? this.nativeImage.getWidth() : this.nativeImage.size?.width + ) : Screen.mainScreen.widthPixels; + + const srcHeight = this.nativeImage ? ( + typeof this.nativeImage.getHeight === 'function' ? this.nativeImage.getHeight() : this.nativeImage.size?.height + ) : Screen.mainScreen.widthPixels; + const requestedSize = getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options); const optionsCopy = { diff --git a/packages/core/image-asset/index.ios.ts b/packages/core/image-asset/index.ios.ts index 072be6fdb1..2888196cee 100644 --- a/packages/core/image-asset/index.ios.ts +++ b/packages/core/image-asset/index.ios.ts @@ -1,6 +1,7 @@ import { ImageAssetBase, getRequestedImageSize } from './image-asset-common'; import { path as fsPath, knownFolders } from '../file-system'; import { queueGC } from '../utils'; +import {Screen} from "../platform"; export * from './image-asset-common'; @@ -36,8 +37,14 @@ export class ImageAsset extends ImageAssetBase { callback(null, 'Asset cannot be found.'); } - const srcWidth = this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth; - const srcHeight = this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight; + const srcWidth = this.nativeImage ? ( + typeof this.nativeImage.getWidth === 'function' ? this.nativeImage.getWidth() : this.nativeImage.size?.width + ) : this.ios.pixelWidth; + + const srcHeight = this.nativeImage ? ( + typeof this.nativeImage.getHeight === 'function' ? this.nativeImage.getHeight() : this.nativeImage.size?.height + ) : this.ios.pixelHeight; + const requestedSize = getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options); if (this.nativeImage) { From f2bdff2a1cf167286ba1fa16f4c15bac4b83bc3c Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 30 May 2025 19:52:37 +0100 Subject: [PATCH 8/8] Use undefined instead of delete for performance --- packages/core/image-asset/image-asset-common.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 07555fca8d..87559a4220 100644 --- a/packages/core/image-asset/image-asset-common.ts +++ b/packages/core/image-asset/image-asset-common.ts @@ -55,7 +55,7 @@ export function getRequestedImageSize(src: { width: number; height: number }, op optionsCopy.width = parsedWidth; } else { console.warn('Invalid width value provided: ', optionsCopy.width); - delete optionsCopy.width; + optionsCopy.width = undefined; } } @@ -65,7 +65,7 @@ export function getRequestedImageSize(src: { width: number; height: number }, op optionsCopy.height = parsedHeight; } else { console.warn('Invalid height value provided: ', optionsCopy.height); - delete optionsCopy.height; + optionsCopy.height = undefined; } }