From 051f60d7496d40decaa2f0c5b1cc5a242f5ffd21 Mon Sep 17 00:00:00 2001 From: Pastoray Date: Fri, 2 May 2025 18:13:34 +0100 Subject: [PATCH 1/2] 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/2] 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; } }