Skip to content

Commit a883a79

Browse files
authored
fix(core): provided image source can be unintentionally disposed (NativeScript#10654)
1 parent 050fa3f commit a883a79

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

packages/core/image-source/index.android.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class ImageSource implements ImageSourceDefinition {
6969
this._rotationAngle = value;
7070
}
7171

72-
constructor(nativeSource?: any) {
72+
constructor(nativeSource?: android.graphics.Bitmap | android.graphics.drawable.Drawable) {
7373
if (nativeSource) {
7474
this.setNativeSource(nativeSource);
7575
}
@@ -301,16 +301,20 @@ export class ImageSource implements ImageSourceDefinition {
301301
return !!this.android;
302302
}
303303

304-
public setNativeSource(source: any): void {
305-
if (source && !(source instanceof android.graphics.Bitmap)) {
306-
if (source instanceof android.graphics.drawable.Drawable) {
307-
this.android = org.nativescript.widgets.Utils.getBitmapFromDrawable(source);
308-
return;
309-
}
304+
public getNativeSource(): android.graphics.Bitmap | android.graphics.drawable.Drawable {
305+
return this.android;
306+
}
307+
308+
public setNativeSource(source: android.graphics.Bitmap | android.graphics.drawable.Drawable): void {
309+
if (!source) {
310+
this.android = null;
311+
} else if (source instanceof android.graphics.Bitmap) {
312+
this.android = source;
313+
} else if (source instanceof android.graphics.drawable.Drawable) {
314+
this.android = org.nativescript.widgets.Utils.getBitmapFromDrawable(source);
315+
} else {
310316
throw new Error('The method setNativeSource() expects an android.graphics.Bitmap or android.graphics.drawable.Drawable instance.');
311317
}
312-
313-
this.android = source;
314318
}
315319

316320
public saveToFile(path: string, format: 'png' | 'jpeg' | 'jpg', quality = 100): boolean {

packages/core/image-source/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ export class ImageSource {
201201
*/
202202
loadFromFontIconCode(source: string, font: Font, color: Color): boolean;
203203

204+
/**
205+
* Gets the native source object (typically a Bitmap or a UIImage).
206+
*/
207+
getNativeSource(): any;
208+
204209
/**
205210
* Sets the provided native source object (typically a Bitmap or a UIImage).
206211
* This will update either the android or ios properties, depending on the target os.

packages/core/image-source/index.ios.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class ImageSource implements ImageSourceDefinition {
4949
// compatibility with Android
5050
}
5151

52-
constructor(nativeSource?: any) {
52+
constructor(nativeSource?: UIImage) {
5353
if (nativeSource) {
5454
this.setNativeSource(nativeSource);
5555
}
@@ -343,14 +343,20 @@ export class ImageSource implements ImageSourceDefinition {
343343
return !!this.ios;
344344
}
345345

346-
public setNativeSource(source: any): void {
347-
if (source && !(source instanceof UIImage)) {
346+
public getNativeSource(): UIImage {
347+
return this.ios;
348+
}
349+
350+
public setNativeSource(source: UIImage): void {
351+
if (!source) {
352+
this.ios = null;
353+
} else if (source instanceof UIImage) {
354+
this.ios = source;
355+
} else {
348356
if (Trace.isEnabled()) {
349357
Trace.write('The method setNativeSource() expects UIImage instance.', Trace.categories.Binding, Trace.messageType.error);
350358
}
351-
return;
352359
}
353-
this.ios = source;
354360
}
355361

356362
public saveToFile(path: string, format: 'png' | 'jpeg' | 'jpg', quality?: number): boolean {

packages/core/ui/image/image-common.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ export abstract class ImageBase extends View implements ImageDefinition {
125125
}
126126
} else if (value instanceof ImageSource) {
127127
// Support binding the imageSource trough the src property
128-
this.imageSource = value;
128+
129+
// This will help avoid cleanup on the actual provided image source in case view gets disposed
130+
this.imageSource = new ImageSource(value.getNativeSource());
129131
this.isLoading = false;
130132
} else if (value instanceof ImageAsset) {
131133
ImageSource.fromAsset(value).then((result) => {

0 commit comments

Comments
 (0)