mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactor(image-source): throw if source is not a correct native instance (#5273)
* refactor(image-source): throw if source is not a correct native instance * refactor(image-source): fromNativeSource and setNativeSource methods * refactor: update image-source.d.ts file BREAKING CHANGE: Change the return type of `setNativeSource` method from `boolean` to `void`.
This commit is contained in:
@@ -139,9 +139,11 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
});
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
public setNativeSource(source: any): void {
|
||||
if (source && !(source instanceof android.graphics.Bitmap)) {
|
||||
throw new Error("The method setNativeSource() expects android.graphics.Bitmap instance.");
|
||||
}
|
||||
this.android = source;
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality = 100): boolean {
|
||||
@@ -239,8 +241,9 @@ export function fromBase64(source: string): ImageSource {
|
||||
}
|
||||
|
||||
export function fromNativeSource(source: any): ImageSource {
|
||||
const image = new ImageSource();
|
||||
return image.setNativeSource(source) ? image : null;
|
||||
const imageSource = new ImageSource();
|
||||
imageSource.setNativeSource(source);
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
export function fromUrl(url: string): Promise<ImageSourceDefinition> {
|
||||
|
||||
@@ -88,11 +88,11 @@ export class ImageSource {
|
||||
fromBase64(source: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Sets the provided native source object (typically a Bitmap).
|
||||
* 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.
|
||||
* @param source The native image object. Will be either a Bitmap for Android or a UIImage for iOS.
|
||||
*/
|
||||
setNativeSource(source: any): boolean;
|
||||
setNativeSource(source: any): void;
|
||||
|
||||
/**
|
||||
* Saves this instance to the specified file, using the provided image format and quality.
|
||||
|
||||
@@ -118,11 +118,11 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
});
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
if (source instanceof UIImage) {
|
||||
this.ios = source;
|
||||
public setNativeSource(source: any): void {
|
||||
if (source && !(source instanceof UIImage)) {
|
||||
throw new Error("The method setNativeSource() expects UIImage instance.");
|
||||
}
|
||||
return source != null;
|
||||
this.ios = source;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean {
|
||||
@@ -221,8 +221,9 @@ export function fromBase64(source: string): ImageSource {
|
||||
}
|
||||
|
||||
export function fromNativeSource(source: any): ImageSource {
|
||||
const image = new ImageSource();
|
||||
return image.setNativeSource(source) ? image : null;
|
||||
const imageSource = new ImageSource();
|
||||
imageSource.setNativeSource(source);
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
export function fromUrl(url: string): Promise<ImageSource> {
|
||||
|
||||
Reference in New Issue
Block a user