1.0.0 • Published 2 years ago

react-native-image-cropview v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@react-native-image-cropview

A React Native module that allows you to crop photos, built with react native Animated api and react-native-gesture-handler.

AndroidiOS

Getting started

$ npm install react-native-image-cropview --save

or

$ yarn add react-native-image-cropview

Additional steps

If you have react-native-gesture-handler installed ignore this step.

$ npm install react-native-gesture-handler --save

or

$ yarn add react-native-gesture-handler

IOS

No additional step is required.

Android

No additional step is required.

Usage

Import Cropper from react-native-image-cropview:

import { Cropper } from 'react-native-image-cropview';

Create state which will be used to keep the image uri or import from other source

const [imageUri, setImageUri] = useState();

Add Cropper like this:

{
  !!imageUri && (
    <Cropper
      uri={imageUri}
      onDone={onCropDone}
      onCancel={onCropCancel}
      onReset={onReset}
      getImageSize={getImageSize}
    />
  )
}

See Options for further information on options.

If you want to call done/cancel/reset programmatically, pass ref to Cropper:

const cropperRef = useRef();

function done() {
  cropperRef.current.done();
}

function cancel() {
  cropperRef.current.cancel();
}

function reset() {
  cropperRef.current.reset();
}

function getImageSize() {
  // Use any method to find the original image width and height
  return {
    width,
    height
  }
}

return (
  <Cropper
    ref={cropperRef}
    uri={imageUri}
    onDone={onCropDone}
    onCancel={onCropCancel}
    onReset={onReset}
    hideFooter={true}
    getImageSize={getImageSize}
  />
)

See Available Methods for further information.

Done callback will be called with a response object, refer to Response Object.

Options

OptionRequiredDescription
uriYesShow the image specified by the URI param.
getImageSizeYesA method to provide the original image width and height to the cropper. Feel free to use any method to get the original image width and height; Example: https://www.npmjs.com/package/react-native-image-size
onDoneNoCallback invoked when cropping is done
onCancelNoCallback invoked on cancel
onResetNoCallback invoked on cropping reset
aspectRatioNoThe aspect ratio of cropping area: Example: original, 4/3, 2/3, 16/9 ...
roundedNoUse round cropping area
scaleMaxNoMaximum image scale of the image. Example: scaleMax={3} where scale scaleMax <= 20 and scaleMax >= 3
hideFooterNoHide all the cropper actions

Response Object

keyDescription
widthwidth of cropping rectangle relative to the original image size
heightheight of cropping rectangle relative to the original image size
xx position of cropping rectangle relative to the original image size
yy position of cropping rectangle relative to the original image size

Available Methods

MethodDescription
doneProgrammatically call cropping done
cancelProgrammatically call cancel
resetProgrammatically call reset

For more advanced usage check our example app.