Content-Length: 5456 | pFad | http://github.com/NativeScript/NativeScript/pull/10654.patch

thub.com From 6435ce919d9e4d374c8c953a2c0b2aa510d5a6e5 Mon Sep 17 00:00:00 2001 From: Dimitris - Rafail Katsampas Date: Sat, 23 Nov 2024 22:32:34 +0200 Subject: [PATCH 1/2] fix(core): Provided image source gets unintentionally disposed --- packages/core/image-source/index.android.ts | 20 ++++++++++++-------- packages/core/image-source/index.d.ts | 5 +++++ packages/core/image-source/index.ios.ts | 16 +++++++++++----- packages/core/ui/image/image-common.ts | 4 +++- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/core/image-source/index.android.ts b/packages/core/image-source/index.android.ts index d4836484e9..d07ccfd3a0 100644 --- a/packages/core/image-source/index.android.ts +++ b/packages/core/image-source/index.android.ts @@ -301,16 +301,20 @@ export class ImageSource implements ImageSourceDefinition { return !!this.android; } - public setNativeSource(source: any): void { - if (source && !(source instanceof android.graphics.Bitmap)) { - if (source instanceof android.graphics.drawable.Drawable) { - this.android = org.nativescript.widgets.Utils.getBitmapFromDrawable(source); - return; - } + public getNativeSource(): android.graphics.Bitmap | android.graphics.drawable.Drawable { + return this.android; + } + + public setNativeSource(source: android.graphics.Bitmap | android.graphics.drawable.Drawable): void { + if (!source) { + this.android = null; + } else if (source instanceof android.graphics.Bitmap) { + this.android = source; + } else if (source instanceof android.graphics.drawable.Drawable) { + this.android = org.nativescript.widgets.Utils.getBitmapFromDrawable(source); + } else { throw new Error('The method setNativeSource() expects an android.graphics.Bitmap or android.graphics.drawable.Drawable instance.'); } - - this.android = source; } public saveToFile(path: string, format: 'png' | 'jpeg' | 'jpg', quality = 100): boolean { diff --git a/packages/core/image-source/index.d.ts b/packages/core/image-source/index.d.ts index 963c993b4a..2974c430c6 100644 --- a/packages/core/image-source/index.d.ts +++ b/packages/core/image-source/index.d.ts @@ -201,6 +201,11 @@ export class ImageSource { */ loadFromFontIconCode(source: string, font: Font, color: Color): boolean; + /** + * Gets the native source object (typically a Bitmap or a UIImage). + */ + getNativeSource(): any; + /** * Sets the provided native source object (typically a Bitmap or a UIImage). * This will update either the android or ios properties, depending on the target os. diff --git a/packages/core/image-source/index.ios.ts b/packages/core/image-source/index.ios.ts index b47414a78d..9f632a546e 100644 --- a/packages/core/image-source/index.ios.ts +++ b/packages/core/image-source/index.ios.ts @@ -49,7 +49,7 @@ export class ImageSource implements ImageSourceDefinition { // compatibility with Android } - constructor(nativeSource?: any) { + constructor(nativeSource?: UIImage) { if (nativeSource) { this.setNativeSource(nativeSource); } @@ -343,14 +343,20 @@ export class ImageSource implements ImageSourceDefinition { return !!this.ios; } - public setNativeSource(source: any): void { - if (source && !(source instanceof UIImage)) { + public getNativeSource(): UIImage { + return this.ios; + } + + public setNativeSource(source: UIImage): void { + if (!source) { + this.ios = null; + } else if (source instanceof UIImage) { + this.ios = source; + } else { if (Trace.isEnabled()) { Trace.write('The method setNativeSource() expects UIImage instance.', Trace.categories.Binding, Trace.messageType.error); } - return; } - this.ios = source; } public saveToFile(path: string, format: 'png' | 'jpeg' | 'jpg', quality?: number): boolean { diff --git a/packages/core/ui/image/image-common.ts b/packages/core/ui/image/image-common.ts index e945665dd3..ab3d221e8b 100644 --- a/packages/core/ui/image/image-common.ts +++ b/packages/core/ui/image/image-common.ts @@ -123,7 +123,9 @@ export abstract class ImageBase extends View implements ImageDefinition { } } else if (value instanceof ImageSource) { // Support binding the imageSource trough the src property - this.imageSource = value; + + // This will help avoid cleanup on the actual provided image source in case view gets disposed + this.imageSource = new ImageSource(value.getNativeSource()); this.isLoading = false; } else if (value instanceof ImageAsset) { ImageSource.fromAsset(value).then((result) => { From 7469562ff194407ec37d8e37deb6bd5ef9a95a04 Mon Sep 17 00:00:00 2001 From: Dimitris - Rafail Katsampas Date: Sun, 24 Nov 2024 08:13:30 +0200 Subject: [PATCH 2/2] chore: Added argument type --- packages/core/image-source/index.android.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/image-source/index.android.ts b/packages/core/image-source/index.android.ts index d07ccfd3a0..b0c3f4ffd2 100644 --- a/packages/core/image-source/index.android.ts +++ b/packages/core/image-source/index.android.ts @@ -69,7 +69,7 @@ export class ImageSource implements ImageSourceDefinition { this._rotationAngle = value; } - constructor(nativeSource?: any) { + constructor(nativeSource?: android.graphics.Bitmap | android.graphics.drawable.Drawable) { if (nativeSource) { this.setNativeSource(nativeSource); }








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/NativeScript/pull/10654.patch

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy