Skip to content

A super simple image cropping and rotation tool for Expo that runs on iOS, Android and Web!

Notifications You must be signed in to change notification settings

RodriguesCosta/expo-image-editor

 
 

Repository files navigation

🌁 Expo Image Editor

A super simple image cropping and rotation tool for Expo that runs on iOS, Android and Web!

Screenshot_20201013-161416 Screenshot_20201013-161447 Screenshot_20201013-161347

Check out the demo on Netlify here

Installation

To get started install the package in your Expo project by running:

yarn add expo-image-editor

or

npm i expo-image-editor

Usage

The package exports a single component ImageEditor that can be placed anywhere in your project. This renders a modal that then returns the editing result when it is dismissed.

// ...
import { ImageEditor } from "expo-image-editor";

function App() {
  const [imageUri, setImageUri] = useState(undefined);

  const [editorVisible, setEditorVisible] = useState(false);

  const selectPhoto = async () => {
    // Get the permission to access the camera roll
    const response = await ImagePicker.requestCameraRollPermissionsAsync();
    // If they said yes then launch the image picker
    if (response.granted) {
      const pickerResult = await ImagePicker.launchImageLibraryAsync();
      // Check they didn't cancel the picking
      if (!pickerResult.cancelled) {
        launchEditor(pickerResult.uri);
      }
    } else {
      // If not then alert the user they need to enable it
      Alert.alert(
        "Please enable camera roll permissions for this app in your settings."
      );
    }
  };

  const launchEditor = (uri: string) => {
    // Then set the image uri
    setImageUri(uri);
    // And set the image editor to be visible
    setEditorVisible(true);
  };

  return (
    <View>
      <Image
        style={{ height: 300, width: 300 }}
        source={{ uri: imageData.uri }}
      />
      <Button title="Select Photo" onPress={() => selectPhoto()} />
      <ImageEditor
        visible={editorVisible}
        onCloseEditor={() => setEditorVisible(false)}
        imageUri={imageUri}
        fixedCropAspectRatio={16 / 9}
        lockAspectRatio={aspectLock}
        minimumCropDimensions={{
          width: 100,
          height: 100,
        }}
        onEditingComplete={(result) => {
          setImageData(result);
        }}
      />
    </View>
  );
}

Props

Name Type Description
visible boolean Whether the editor should be visible or not.
onCloseEditor function Callback when the editor is dimissed - use this to set hide the editor.
imageUri string The uri of the image to be edited
fixedCropAspectRatio number The starting aspect ratio of the cropping window.
lockAspectRatio boolean Whether the cropping window should maintain this aspect ratio or not.
minimumCropDimensions object An object of {width, height} specifying the minimum dimensions of the crop window.
onEditingComplete function function that will return the result of the image editing which is an object identical to imageData
throttleBlur boolean Whether to throttle the WebGL update of the blur while adjusting (defaults to false) - useful to set to true on lower performance devices

About

A super simple image cropping and rotation tool for Expo that runs on iOS, Android and Web!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 85.6%
  • JavaScript 9.4%
  • HTML 5.0%