DEV Community

Cover image for How to Compress or Manipulate Images in React Native Expo
Niraj Dhungana
Niraj Dhungana

Posted on

 

How to Compress or Manipulate Images in React Native Expo

Nowadays smartphone cameras are a beast. A normal picture clicked with my smartphone becomes 4MB. And if I were just a normal user this would not be a topic of discussion but because I am a programmer it costs my cloud storage.

So, in this small post I will teach you guys how we can compress the Image size in React Native Expo.

For that you don’t need to do any special coding because expo has a cool npm package which we can use to decrease or compress image size.

Which Package to Use

Inside the expo we have an expo-image-manipulator which we can use to compress image size and also for other manipulations as well like flip, rotate, crop etc. If you want more information on it then you can go to this link.

You can also use it if you are using bare-react-native cli but for that you need to go some extra miles and unfortunately I am not covering that here. You can visit this link if you are using bare-react-native.

First we need to install: expo-image-manipulator. For that you can run this command.

expo install expo-image-manipulator
Enter fullscreen mode Exit fullscreen mode

After installing it we need to import everything as ImageManipulator like below.

import * as ImageManipulator from 'expo-image-manipulator';
Enter fullscreen mode Exit fullscreen mode
const file = await ImageManipulator.manipulateAsync(img.uri, [], { compress: 0.5 });
Enter fullscreen mode Exit fullscreen mode

To manipulate images using expo-image-manipulator it provides the manipulateAsync method. Which is an asynchronous method. It takes three arguments. The actual image uri goes first, actions goes second and save options at the end.

await ImageManipulator.manipulateAsync(uri, actions, saveOptions)
Enter fullscreen mode Exit fullscreen mode

What Kind of Images it Can Manipulate

It does not take the image files from outside. So make sure you are using URI from the local file system not from the remote api link. Means you can only use the image files which you read from the device storage.

Actions

Action is an array of objects representing manipulation options. We can pass multiple options as objects inside this array. Like we can resize, rotate, flip and crop.

Save Options

After manipulating the image we can also define some options and inside it we can specify the image quality and image format like png or jpg.

await ImageManipulator.
manipulateAsync(img.uri, [
{resize: {width: 200, height: 200}},
{rotare: 45},
{flip: ImageManipulator.FlipType.Vertical}
],
{compress: 0.5, format: ImageManipulator.SaveFormat.PNG});
Enter fullscreen mode Exit fullscreen mode

Inside this little example as you can see the first thing that we are passing is image uri, and some action options to resize, rotate and flip the image. And at the end we are passing save options to compressing the image by 50% and converting the image to png format.

For more post like this you can check out this like.

Top comments (0)

Top Posts from the React Ecosystem

1. Changes In The Official React Documentation

The former React Docs Beta has been officially released as the updated React documentation at react.dev after years of hard work and refinement. Check out the brand new React Docs: What’s New in the Updated React Docs

2. CRA's Time is Over

React developer team has removed create-react-app (CRA) from official documentation rendering it no longer the default setup method for new projects. The bulky setup, slow, and outdated nature of CRA led to its removal: create-react-app is officially dead

3. How to Fetch Dev.to Articles for Your Portfolio

Integrate the articles of your Dev.to profile into your personal portfolio with either React, Vue, or Next.js by following these simple steps. It outlines how to include frontend to pull the information and correctly utilizes the Dev.to API: How to Fetch Your Dev.to Articles for Your Portfolio with React