ivpusic / react-native-image-crop-picker

iOS/Android image picker with support for camera, video, configurable compression, multiple images and cropping
MIT License
6.08k stars 1.55k forks source link

Is it possible to directly open image cropper without selecting file from device? #1891

Open tragicmj opened 1 year ago

tragicmj commented 1 year ago

Version

Tell us which versions you are using:

Platform

Tell us to which platform this issue is related Both

Expected behaviour

Want to directly open image cropper without selecting image from device.

Actual behaviour

Select Image from device then only the image crop option is available

Steps to reproduce

1.

2.

3.

Attachments

// stacktrace or any other useful debug info

Love react-native-image-crop-picker? Please consider supporting our collective: 👉 https://opencollective.com/react-native-image-crop-picker/donate

deflexable commented 1 year ago

have you tried the code below?

import ImageCropPicker from "react-native-image-crop-picker";

      ImageCropPicker.openCropper({
            mediaType: 'photo',
            cropping: true,
            freeStyleCropEnabled: true,
            path: 'file:///path/to/image.png',
            avoidEmptySpaceAroundImage: false,
            cropperToolbarTitle: 'Adjust Photo',
            cropperChooseText: 'Done',
            compressImageQuality: 1
      }).then(res => {
             const crop = res.cropRect, // {width:number, height:number}
                     file = res.path;
       }, err=> handleError(err));
tragicmj commented 1 year ago

Hey, thank you so much for this reply. Will try it once tomorrow

tragicmj commented 1 year ago

Tried your solution it works except for one issue, on cancel the cropper is coming again Here: ShowMessage.message is used for toast message & ShowMessage.consoleError for showing error in console

     try {
  const req = []
  const res = await ImagePicker.openCropper({
    cropping: true,
    mediaType: 'photo',
    forceJpg: true,
    freeStyleCropEnabled: true,
    path: this.props.imgUploadLink,
    // cropperToolbarTitle: 'Adjust Photo',
    cropperChooseText: 'Done',
    width: 1000,
    height: 1000,
    compressImageQuality: 1
  })
  let fileExt = ''
  if(Platform.OS === 'ios'){
    fileExt = res.path.split(".").pop()
  } 
    const url = res.path
    let fileBase = await RNFS.readFile(url, 'base64');
    if(fileExt.toLocaleLowerCase() == 'heic'){
      const val = await RNHeicConverter
      .convert({ // options
        path: res.path,
        extension: 'jpg'
      })
      .then(async (result) => {
        // console.log(result); // { success: true, path: "path/to/jpg", error, base64, }
        if(result.success){
          fileBase = await RNFS.readFile(result.path, 'base64')
        }
      });
    }
    if (res.size <= 10485760) {
      const val = {
        file_name: res.path.split("/").pop(),
        image_url: fileBase,
        type: res.mime,
      };
      req.push(val);
    } else {
      ShowMessage.message('Files should be less than 10 MB');
    }
    if (req.length != 0) {
      await this.setState({
        imageFile: fileBase,
        imageLink: res.path
        // Platform.OS === 'android' ? res.path : res.sourceURL,
      });
    }
} catch (error) {
  ShowMessage.consoleError(error);
}

https://user-images.githubusercontent.com/44090234/217000998-ed97b401-a884-4e7a-8798-42e201bfa140.mp4