react-native-image-base64-png
v0.2.1
Published
Convert PNG images to base64
Downloads
39
Readme
react-native-image-base64-png
This is a fork of this library, modified to support png on android and export typescript types.
That repo in turn is a working rewrite of this abandoned library. It provides a very simple way to convert an image to a base64 string.
If you encounter OOM
errors on old android devices, make sure you optimize the image's size before you convert it.
Indeed working with big images on Android might cause very high memory usage.
Getting started
npm install react-native-image-base64-png --save
or
yarn add react-native-image-base64-png
Installation
$ react-native link react-native-image-base64-png
Usage
import ImgToBase64 from 'react-native-image-base64-png';
ImgToBase64.getBase64String('file://youfileurl')
.then(base64String => doSomethingWith(base64String))
.catch(err => doSomethingWith(err));
Changes made
This package has been updated based on this thread.
Simply put, android/src/java/RNImgToBase64Module.java
has been changed as follows:
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
To:
bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
I have also added types in index.d.ts
:
declare module 'react-native-image-base64' {
export default class ImgToBase64 {
static getBase64String(filePath: string): Promise<string>;
}
}