Skip to content

Commit 58d61ca

Browse files
author
Vasil Chimev
authored
refactor(image-source): throw if source is not a correct native instance (NativeScript#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`.
1 parent 1f77301 commit 58d61ca

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

tns-core-modules/image-source/image-source.android.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ export class ImageSource implements ImageSourceDefinition {
139139
});
140140
}
141141

142-
public setNativeSource(source: any): boolean {
142+
public setNativeSource(source: any): void {
143+
if (source && !(source instanceof android.graphics.Bitmap)) {
144+
throw new Error("The method setNativeSource() expects android.graphics.Bitmap instance.");
145+
}
143146
this.android = source;
144-
return source != null;
145147
}
146148

147149
public saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality = 100): boolean {
@@ -239,8 +241,9 @@ export function fromBase64(source: string): ImageSource {
239241
}
240242

241243
export function fromNativeSource(source: any): ImageSource {
242-
const image = new ImageSource();
243-
return image.setNativeSource(source) ? image : null;
244+
const imageSource = new ImageSource();
245+
imageSource.setNativeSource(source);
246+
return imageSource;
244247
}
245248

246249
export function fromUrl(url: string): Promise<ImageSourceDefinition> {

tns-core-modules/image-source/image-source.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ export class ImageSource {
8888
fromBase64(source: string): Promise<boolean>;
8989

9090
/**
91-
* Sets the provided native source object (typically a Bitmap).
91+
* Sets the provided native source object (typically a Bitmap or a UIImage).
9292
* This will update either the android or ios properties, depending on the target os.
9393
* @param source The native image object. Will be either a Bitmap for Android or a UIImage for iOS.
9494
*/
95-
setNativeSource(source: any): boolean;
95+
setNativeSource(source: any): void;
9696

9797
/**
9898
* Saves this instance to the specified file, using the provided image format and quality.

tns-core-modules/image-source/image-source.ios.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ export class ImageSource implements ImageSourceDefinition {
118118
});
119119
}
120120

121-
public setNativeSource(source: any): boolean {
122-
if (source instanceof UIImage) {
123-
this.ios = source;
121+
public setNativeSource(source: any): void {
122+
if (source && !(source instanceof UIImage)) {
123+
throw new Error("The method setNativeSource() expects UIImage instance.");
124124
}
125-
return source != null;
125+
this.ios = source;
126126
}
127127

128128
public saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean {
@@ -221,8 +221,9 @@ export function fromBase64(source: string): ImageSource {
221221
}
222222

223223
export function fromNativeSource(source: any): ImageSource {
224-
const image = new ImageSource();
225-
return image.setNativeSource(source) ? image : null;
224+
const imageSource = new ImageSource();
225+
imageSource.setNativeSource(source);
226+
return imageSource;
226227
}
227228

228229
export function fromUrl(url: string): Promise<ImageSource> {

0 commit comments

Comments
 (0)