Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to optimise an Image in React Native

Tags:

Photos captured via camera are too large for efficient upload and download in React native.

Is there an api or library to compress a PNG image file in React Native?

like image 599
Learner Avatar asked Jun 05 '16 07:06

Learner


People also ask

How do I compress an image in react?

We can resize, compress and convert the images based on our requirements. To compress your images follow these three simple steps: Install the package using npm/yarn. Add the particular code block to your project.


2 Answers

If you are using react-native-image-picker for uploading images, you can set maxWidth, maxHeight or quality of image for reducing the size.

 const options = {
                    title: 'Select Picture',
                    storageOptions: {
                        skipBackup: true,
                        path: 'images',

                    },
                    maxWidth: 500,
                    maxHeight: 500,
                    quality: 0.5
                };
ImagePicker.showImagePicker(options, resolve, reject);
like image 116
Shehzad Osama Avatar answered Oct 24 '22 00:10

Shehzad Osama


https://github.com/bamlab/react-native-image-resizer provides an API to resize local images.

It allows you to specify:

  • Max dimensions (whilst preserving aspect ratio) and;
  • Output quality (for JPEG only)

API

import ImageResizer from 'react-native-image-resizer';

ImageResizer.createResizedImage(imageUri, newWidth, newHeight, compressFormat, quality).then((resizedImageUri) => {
  // resizeImageUri is the URI of the new image that can now be displayed, uploaded...
}).catch((err) => {
  // Oops, something went wrong. Check that the filename is correct and
  // inspect err to get more details.
});
like image 38
Learner Avatar answered Oct 24 '22 01:10

Learner