1- import { ImageAssetBase , getRequestedImageSize } from ' ./image-asset-common' ;
2- import { path as fsPath , knownFolders } from ' ../file-system' ;
3- import { ad } from ' ../utils' ;
4- import { Screen } from ' ../platform' ;
5- export * from ' ./image-asset-common' ;
1+ import { ImageAssetBase , getRequestedImageSize } from " ./image-asset-common" ;
2+ import { path as fsPath , knownFolders } from " ../file-system" ;
3+ import { ad } from " ../utils" ;
4+ import { Screen } from " ../platform" ;
5+ export * from " ./image-asset-common" ;
66
77export class ImageAsset extends ImageAssetBase {
88 private _android : string ; //file name of the image
99
1010 constructor ( asset : string ) {
1111 super ( ) ;
12- let fileName = typeof asset === ' string' ? asset . trim ( ) : '' ;
13- if ( fileName . indexOf ( '~/' ) === 0 ) {
14- fileName = fsPath . join ( knownFolders . currentApp ( ) . path , fileName . replace ( '~/' , '' ) ) ;
12+ let fileName = typeof asset === " string" ? asset . trim ( ) : "" ;
13+ if ( fileName . indexOf ( "~/" ) === 0 ) {
14+ fileName = fsPath . join ( knownFolders . currentApp ( ) . path , fileName . replace ( "~/" , "" ) ) ;
1515 }
1616 this . android = fileName ;
1717 }
@@ -25,7 +25,58 @@ export class ImageAsset extends ImageAssetBase {
2525 this . _android = value ;
2626 }
2727
28- public getImageAsync ( callback : ( image , error ) => void ) {
28+ /**
29+ * Validates and adjusts image dimensions to prevent bitmap size exceeding Android's 32-bit limit.
30+ * Android has a limitation where bitmap size (width * height) cannot exceed 2^31-1.
31+ * This method ensures the dimensions are within safe bounds while maintaining aspect ratio.
32+ *
33+ * @param {number|string } width - The desired width of the image
34+ * @param {number|string } height - The desired height of the image
35+ * @returns {{ width: number; height: number } } Object containing validated dimensions
36+ *
37+ * @example
38+ * // Returns safe dimensions that won't exceed Android's bitmap size limit
39+ * const safe = validateDimensions("4000", "3000");
40+ */
41+ private _validateDimensions ( width : number | string , height : number | string ) : { width : number ; height : number } {
42+ const parseSize = ( size : number | string ) : number => {
43+ return typeof size === "string" ? parseInt ( size , 10 ) : size ;
44+ } ;
45+
46+ let w = parseSize ( width ) ;
47+ let h = parseSize ( height ) ;
48+
49+ // Check for 32-bit limitation (2^31 - 1, leaving some headroom)
50+ const MAX_DIMENSION = Math . floor ( Math . sqrt ( Math . pow ( 2 , 31 ) - 1 ) ) ;
51+
52+ // Check if each dimension exceeds MAX_DIMENSION
53+ w = Math . min ( w , MAX_DIMENSION ) ;
54+ h = Math . min ( h , MAX_DIMENSION ) ;
55+
56+ // Check the total pixel count
57+ if ( w * h > Math . pow ( 2 , 31 ) - 1 ) {
58+ const scale = Math . sqrt ( ( Math . pow ( 2 , 31 ) - 1 ) / ( w * h ) ) ;
59+ w = Math . floor ( w * scale ) ;
60+ h = Math . floor ( h * scale ) ;
61+ }
62+
63+ return { width : w , height : h } ;
64+ }
65+
66+ /**
67+ * Asynchronously loads an image with the specified dimensions.
68+ * Handles string/number dimension types and applies size validation for Android bitmap limitations.
69+ *
70+ * @param {Function } callback - Callback function that receives (image, error)
71+ * @param {{ width?: number; height?: number } } [options] - Optional dimensions for the image
72+ */
73+ public getImageAsync ( callback : ( image , error ) => void , options ?: { width ?: number ; height ?: number } ) {
74+ if ( options ?. width || options ?. height ) {
75+ const validDimensions = this . _validateDimensions ( options . width || this . options . width || 0 , options . height || this . options . height || 0 ) ;
76+ options . width = validDimensions . width ;
77+ options . height = validDimensions . height ;
78+ }
79+
2980 org . nativescript . widgets . Utils . loadImageAsync (
3081 ad . getApplicationContext ( ) ,
3182 this . android ,
@@ -39,7 +90,7 @@ export class ImageAsset extends ImageAssetBase {
3990 onError ( ex ) {
4091 callback ( null , ex ) ;
4192 } ,
42- } )
93+ } ) ,
4394 ) ;
4495 }
4596}
0 commit comments