diff --git a/packages/core/image-asset/image-asset-common.ts b/packages/core/image-asset/image-asset-common.ts index 798727dd3e..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; @@ -47,8 +48,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 { + Trace.write('Invalid width value provided: ' + optionsCopy.width, Trace.categories.Debug, Trace.messageType.warn); + delete optionsCopy.width; + } + } + + if (typeof optionsCopy.height === 'string') { + const parsedHeight = parseInt(optionsCopy.height, 10); + if (!isNaN(parsedHeight)) { + optionsCopy.height = parsedHeight; + } else { + Trace.write('Invalid height value provided: ' + optionsCopy.height, Trace.categories.Debug, Trace.messageType.warn); + 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); }, - }) + }), ); } }