diff --git a/packages/core/image-source/index.android.ts b/packages/core/image-source/index.android.ts index d4836484e..b0c3f4ffd 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); } @@ -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 963c993b4..2974c430c 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 b47414a78..9f632a546 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 0ab3148c5..7ef7ddf9d 100644 --- a/packages/core/ui/image/image-common.ts +++ b/packages/core/ui/image/image-common.ts @@ -125,7 +125,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) => {