diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 798727dd3e..87559a4220 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 = { ...(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); + optionsCopy.width = undefined; + } + } + + if (typeof optionsCopy.height === 'string') { + const parsedHeight = parseInt(optionsCopy.height, 10); + if (!isNaN(parsedHeight)) { + optionsCopy.height = parsedHeight; + } else { + console.warn('Invalid height value provided: ', optionsCopy.height); + optionsCopy.height = undefined; + } + } + + 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); diff --git a/packages/core/image-asset/index.android.ts b/packages/core/image-asset/index.android.ts index 055caeff60..4e4888597d 100644 --- a/packages/core/image-asset/index.android.ts +++ b/packages/core/image-asset/index.android.ts @@ -26,10 +26,31 @@ 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 ? ( + 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 = { + ...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 +60,7 @@ export class ImageAsset extends ImageAssetBase { onError(ex) { callback(null, ex); }, - }) + }), ); } } 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; } 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) {