Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(android) handle width height as strings in image-asset #10736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
26 changes: 24 additions & 2 deletions packages/core/image-asset/image-asset-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 23 additions & 2 deletions packages/core/image-asset/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -39,7 +60,7 @@ export class ImageAsset extends ImageAssetBase {
onError(ex) {
callback(null, ex);
},
})
}),
);
}
}
4 changes: 2 additions & 2 deletions packages/core/image-asset/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 9 additions & 2 deletions packages/core/image-asset/index.ios.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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) {
Expand Down