mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 10:01:08 +08:00
fix(core): provided image source can be unintentionally disposed (#10654)
This commit is contained in:

committed by
GitHub

parent
050fa3fea6
commit
a883a79e3b
@ -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 {
|
||||
|
5
packages/core/image-source/index.d.ts
vendored
5
packages/core/image-source/index.d.ts
vendored
@ -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.
|
||||
|
@ -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 {
|
||||
|
@ -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) => {
|
||||
|
Reference in New Issue
Block a user