Skip to content
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