hhunaid / react-native-image-crop-tools

Native-ish Image Crop Tools for react native
173 stars 60 forks source link

How to get saved file path #20

Open rajscet opened 3 years ago

mohamed2m2018 commented 3 years ago

when you call the function saveImage, you can get the file data from this line
onImageCrop={(res) => console.warn(res)} Which you supply to the CropView component, and where res is your file data which includes the path

you will have something like that {"height": 264, "target": 6539, "uri": "file:///Users/mohamedsalah/Library/Developer/CoreSimulator/Devices/E13A700A-8CA3-4786-9F15-20606DDF6CF4/data/Containers/Data/Application/953702B2-9FB2-4B10-B263-416CA8A73ADD/Library/Caches/077489DE-2405-403D-8902-DC7EC908487D.jpg", "width": 264}

ajaymarathe commented 3 years ago

Hey @mohamed2m2018 I am not getting in response in After I clicked on "Get Cropped View" Button, here is what i m doing ->

import React, { useRef } from 'react';
import { Button, StatusBar, StyleSheet, View, Image } from 'react-native';
import { CropView } from 'react-native-image-crop-tools';
import Modal from 'react-native-modal';

const CropImage = ({ isVisible, uri, onClose }) => {
  const cropViewRef = useRef();
  return (
    <>
      <View style={{ flex: 1 }}>
        <Modal isVisible={isVisible}>
          <View style={{ flex: 1 }}>
            <StatusBar barStyle="dark-content" />
            <View style={styles.container}>
              <CropView
                sourceUrl={uri}
                style={styles.cropView}
                ref={cropViewRef}
                onImageCrop={(res) => {
                  console.log('res: ', res); // not getting any response here, don't know what's wrong.
                }}
                keepAspectRatio
                aspectRatio={{ width: 16, height: 16 }}
              />
              <Button
                title={'Get Cropped View'}
                onPress={() => {
                  console.log('corp');
                  cropViewRef.current.saveImage(90);
                }}
              />
            </View>
          </View>
        </Modal>
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  cropView: {
    flex: 1,
  },
});

export default CropImage;
dandresfg commented 3 years ago

Yo have to use cropViewRef.current.saveImage(true, 90);

rililive commented 2 years ago

Even after getting the response, I get a file location like this {"height": 708, "uri": "/data/user/0/app.therrmobile/cache/temp_file_.jpg", "width": 708}

That is problematic because subsequent cropped image display the image from the first time cropping. I'm not sure if the path is getting cached or what. Any way to cache bust?

Update:

For image preview cachebusting, I just added some random query params to end of file path ${filepath}?cacheBust=${Math.random()}

amnbhr commented 2 years ago

you need to add 'file://' in the first of uri that you get in response after crop for example: {"height": 708, "uri": "/data/user/0/app.therrmobile/cache/tempfile.jpg", "width": 708} filepath: file:///data/user/0/app.therrmobile/cache/tempfile.jpg

react06 commented 1 year ago

@ajaymarathe just replace this onPress={() => { console.log('corp'); cropViewRef.current.saveImage(90); }} in your code with onPress={() => cropViewRef.current.saveImage(90)}