Image Source
In this article, we will look at different ways to load or save image while using ImageSource module.
The pre-required imageSource module is used throughout the following code snippets. We also use fs module defined as follows:
Load Image
Load image using resource name
This is similar to loading Bitmap from R.drawable.logo on Android or calling [UIImage imageNamed@"logo"] on iOS. The method fromResource creates an ImageSource instance and loads it from the specified resource name.
Load image from a local file
Use fromFile(path: string): Promise
Creating PNG image file from base64 string
The method fromBase64(source: string): Promise
Save Image
Save image to PNG or JPG file
The method saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean saves ImageSource instance to the specified file, using the provided image format and quality. The supported formats are png, jpeg, and jpg. The quality parameter is optional and defaults to maximum quality. Returns true if the file is saved successfully.
Save image from image asset to PNG file
Use fromAsset(asset: ImageAsset): Promise
Creating base64 string from PNG image file
The method toBase64String(format: "png" | "jpeg" | "jpg", quality?: number): string converts the image to base64 encoded string, using the provided image format and quality. The supported formats are png, jpeg, and jpg. The quality parameter is optional and defaults to maximum quality.
API Reference for the Image Source Class