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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/core/image-asset/image-asset-common.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 || {}) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the pr @Pastoray, it'd be good to have the ImageAssetOptions reflect that too and likely need to parse it on ios as well:


should be number | string


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);
Expand Down
19 changes: 17 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,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({
Expand All @@ -39,7 +54,7 @@ export class ImageAsset extends ImageAssetBase {
onError(ex) {
callback(null, ex);
},
})
}),
);
}
}