0.2.6 • Published 3 years ago

react-native-amazing-cropper v0.2.6

Weekly downloads
431
License
MIT
Repository
github
Last release
3 years ago

react-native-amazing-cropper

Image cropper for react native made with Animated API (with rotation possibility) - for iOS & android

         

         

This component depend on @bam.tech/react-native-image-resizer, @wili/react-native-image-rotate and @react-native-community/image-editor libraries. They need to be installed and linked to your project before.

STEPS TO INSTALL: 1. npm install --save @bam.tech/react-native-image-resizer @wili/react-native-image-rotate @react-native-community/image-editor 2. react-native link @bam.tech/react-native-image-resizer @wili/react-native-image-rotate @react-native-community/image-editor 3. npm install --save react-native-amazing-cropper

Step 2 is not needed for react-native >= 0.60 because of autolinking. Instead run pod install inside ios directory.

Properties


PropTypeDescription
onDonefunctionA function which accepts 2 arguments: croppedImageUri and garbageUris. garbageUris is an array of string (it has 1 or 2 uri) and should be ignored. It is returned only to give you the possibility to clear the garbage uris from cache (ex. using unlink method from react-native-fs; see this issue). Called when user press the 'DONE' button
onErrorfunctionA function which accepts 1 argument err of type Error. Called when rotation or cropping fails
onCancelfunctionA function without arguments. Called when user press the 'CANCEL' button
imageUristringThe uri of the image you want to crop or rotate
imageWidthnumberThe width (in pixels) of the image you passed in imageUri
imageHeightnumberThe height (in pixels) of the image you passed in imageUri
initialRotationnumberNumber which set the default rotation of the image when cropper is initialized. Default is 0
footerComponentcomponentCustom component for footer. Default is <DefaultFooter doneText='DONE' rotateText='ROTATE' cancelText='CANCEL' />
NOT_SELECTED_AREA_OPACITYnumberThe opacity of the area which is not selected by the cropper. Should be a value between 0 and 1. Default is 0.5
BORDER_WIDTHnumberThe border width (see image). Default is 50
COMPONENT_WIDTHnumberThe width of the entire component. Default is Dimensions.get('window').width, not recommended to change.
COMPONENT_HEIGHTnumberThe height of the entire component. Default is Dimensions.get('window').height, you should change it to fix hidden footer issue.

Usage example 1 (using the default footer)


import React, { Component } from 'react';
import AmazingCropper from 'react-native-amazing-cropper';;

class AmazingCropperPage extends Component {
  onDone = (croppedImageUri, garbageUris) => {
    console.log('croppedImageUri = ', croppedImageUri);
    // clear the garbage uris from cache
    // send image to server for example
  }

  onError = (err) => {
    console.log(err);
  }

  onCancel = () => {
    console.log('Cancel button was pressed');
    // navigate back
  }

  render() {
    return (
      <AmazingCropper
        onDone={this.onDone}
        onError={this.onError}
        onCancel={this.onCancel}
        imageUri='https://www.lifeofpix.com/wp-content/uploads/2018/09/manhattan_-11-1600x2396.jpg'
        imageWidth={1600}
        imageHeight={2396}
        NOT_SELECTED_AREA_OPACITY={0.3}
        BORDER_WIDTH={20}
      />
    );
  }
}

Usage example 2 (using the default footer with custom text)


import React, { Component } from 'react';
import AmazingCropper, { DefaultFooter } from 'react-native-amazing-cropper';

class AmazingCropperPage extends Component {
  onDone = (croppedImageUri, garbageUris) => {
    console.log('croppedImageUri = ', croppedImageUri);
    // clear the garbage uris from cache
    // send image to server for example
  }

  onError = (err) => {
    console.log(err);
  }

  onCancel = () => {
    console.log('Cancel button was pressed');
    // navigate back
  }

  render() {
    return (
      <AmazingCropper
        // Pass custom text to the default footer
        footerComponent={<DefaultFooter doneText='OK' rotateText='ROT' cancelText='BACK' />}
        onDone={this.onDone}
        onError={this.onError}
        onCancel={this.onCancel}
        imageUri='https://www.lifeofpix.com/wp-content/uploads/2018/09/manhattan_-11-1600x2396.jpg'
        imageWidth={1600}
        imageHeight={2396}
      />
    );
  }
}

Usage example 3 (using own fully customized footer)


Write your custom footer component. Don't forget to call the props.onDone, props.onRotate and props.onCancel methods inside it (the Cropper will pass them automatically to your footer component). Example of custom footer component:

import React from 'react';
import { View, TouchableOpacity, Text, Platform, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';
import MaterialCommunityIcon from 'react-native-vector-icons/MaterialCommunityIcons';

const CustomCropperFooter = (props) => (
  <View style={styles.buttonsContainer}>
    <TouchableOpacity onPress={props.onCancel} style={styles.touchable}>
      <Text style={styles.text}>CANCEL</Text>
    </TouchableOpacity>
    <TouchableOpacity onPress={props.onRotate} style={styles.touchable}>
      <MaterialCommunityIcon name='format-rotate-90' style={styles.rotateIcon} />
    </TouchableOpacity>
    <TouchableOpacity onPress={props.onDone} style={styles.touchable}>
      <Text style={styles.text}>DONE</Text>
    </TouchableOpacity>
  </View>
)

export default CustomCropperFooter;

CustomCropperFooter.propTypes = {
  onDone: PropTypes.func,
  onRotate: PropTypes.func,
  onCancel: PropTypes.func
}

const styles = StyleSheet.create({
  buttonsContainer: {
    flexDirection: 'row',
    alignItems: 'center', // 'flex-start'
    justifyContent: 'space-between',
    height: '100%'
  },
  text: {
    color: 'white',
    fontSize: 16
  },
  touchable: {
    padding: 10,
  },
  rotateIcon: {
    color: 'white',
    fontSize: 26,
    ...Platform.select({
      android: {
        textShadowOffset: { width: 1, height: 1 },
        textShadowColor: '#000000',
        textShadowRadius: 3,
        shadowOpacity: 0.9,
        elevation: 1
      },
      ios: {
        shadowOffset: { width: 1, height: 1 },
        shadowColor: '#000000',
        shadowRadius: 3,
        shadowOpacity: 0.9
      }
    }),
  },
})

Now just pass your footer component to the Cropper like here:

import React, { Component } from 'react';
import AmazingCropper from 'react-native-amazing-cropper';
import CustomCropperFooter from './src/components/CustomCropperFooter.component';

class AmazingCropperPage extends Component {
  onDone = (croppedImageUri, garbageUris) => {
    console.log('croppedImageUri = ', croppedImageUri);
    // clear the garbage uris from cache
    // send image to server for example
  }

  onError = (err) => {
    console.log(err);
  }

  onCancel = () => {
    console.log('Cancel button was pressed');
    // navigate back
  }

  render() {
    return (
      <AmazingCropper
        // Use your custom footer component
        // Do NOT pass onDone, onRotate and onCancel to the footer component, the Cropper will do it for you
        footerComponent={<CustomCropperFooter />}
        onDone={this.onDone}
        onError={this.onError}
        onCancel={this.onCancel}
        imageUri='https://www.lifeofpix.com/wp-content/uploads/2018/09/manhattan_-11-1600x2396.jpg'
        imageWidth={1600}
        imageHeight={2396}
      />
    );
  }
}

Do you need a mobile app for android & iOS? Hire me

0.2.1

3 years ago

0.2.0

3 years ago

0.2.6

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.1.8

3 years ago

0.1.9

3 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

5 years ago

0.1.2

5 years ago

0.1.3

5 years ago

0.1.1

5 years ago

0.1.0

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago