Content-Length: 386367 | pFad | http://github.com/NativeScript/NativeScript/pull/10654/files

85 fix(core): Provided image source gets unintentionally disposed by CatchABus · Pull Request #10654 · NativeScript/NativeScript · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Provided image source gets unintentionally disposed #10654

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packages/core/image-source/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/image-source/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 11 additions & 5 deletions packages/core/image-source/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class ImageSource implements ImageSourceDefinition {
// compatibility with Android
}

constructor(nativeSource?: any) {
constructor(nativeSource?: UIImage) {
if (nativeSource) {
this.setNativeSource(nativeSource);
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/ui/image/image-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading








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/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy