4

I am going to rotate the image in react–native and I would like to get base64 of rotated image. I used several libraries

  1. react-native-image-rotate: It's working well on Android but on iOS I get rct-image-store://1 as url so I tried getting base64 using rn-fetch-blob but it throws error that can't recognize that url.

  2. react-native-image-resizer: I used this but the response is not good in iOS. If I set -90 then rotate -180, if I set -180 then it's rotating as -270.

Please help me on this problem, how can I rotate the image in iOS.

I need to rotate the image as -90, -180, -270, -360(original).

4

3 回答 3

3

最后,我找到了答案。

import ImageRotate from 'react-native-image-rotate';
import ImageResizer from 'react-native-image-resizer';
import RNFetchBlob from 'rn-fetch-blob';

ImageRotate.rotateImage(
        this.state.image.uri,
        rotateDegree,
        uri => {
          if (Platform.OS === 'android') {
            console.log('rotate', uri);
            RNFetchBlob.fs.readFile(uri, 'base64').then(data => {
              const object = {};
              object.base64 = data;
              object.width = this.state.image.height;
              object.height = this.state.image.width;
              object.uri = uri;
              this.setState({image: object, spinner: false});
            });
          } else {
            console.log(uri);
            const outputPath = `${RNFetchBlob.fs.dirs.DocumentDir}`;

            ImageResizer.createResizedImage(
              uri,
              this.state.image.height,
              this.state.image.width,
              'JPEG',
              100,
              0,
              outputPath,
            ).then(response => {
              console.log(response.uri, response.size);
              let imageUri = response.uri;
              if (Platform.OS === 'ios') {
                imageUri = imageUri.split('file://')[1];
              }
              RNFetchBlob.fs.readFile(imageUri, 'base64').then(resData => {
                const object = {};
                object.base64 = resData;
                object.width = this.state.image.height;
                object.height = this.state.image.width;
                object.uri = response.uri;
                this.setState({image: object, spinner: false});
              });
            });
          }
        },
        error => {
          console.error(error);
        },
      );
    }
于 2020-01-16T19:05:43.220 回答
0

尝试使用 Expo Image Manipulator https://docs.expo.io/versions/latest/sdk/imagemanipulator/

  const rotated = await ImageManipulator.manipulateAsync(
    image.uri,
    [{ rotate: -90 }],
    { base64: true }
  );
于 2021-06-10T13:38:48.743 回答
0

这是我到目前为止的工作良好代码

  rotateImage = (angle) => {
    const { currentImage } = this.state; // origin Image, you can pass it from params,... as you wish
    ImageRotate.rotateImage( // using 'react-native-image-rotate'
      currentImage.uri,
      angle,
      (rotatedUri) => {
        if (Platform.OS === 'ios') {
          ImageStore.getBase64ForTag( // import from react-native 
            rotatedUri,
            (base64Image) => {
              const imagePath = `${RNFS.DocumentDirectoryPath}/${new Date().getTime()}.jpg`;
              RNFS.writeFile(imagePath, `${base64Image}`, 'base64') // using 'react-native-fs'
                .then(() => {
                  // now your file path is imagePath (which is a real path)
                  if (success) {
                    this.updateCurrentImage(imagePath, currentImage.height, currentImage.width);
                    ImageStore.removeImageForTag(rotatedUri);
                  }
                })
                .catch(() => {});
            },
            () => {},
          );
        } else {
          this.updateCurrentImage(rotatedUri, currentImage.height, currentImage.width);
        }
      },
      (error) => {
        console.error(error);
      },
    );
  };

我想你已经做到了rotatedUri

  • ImageStore然后你可以通过from获得 base64 react-native
  • 然后将其写入本地图像react-native-fs

之后,您拥有imagePath的是本地图像。

于 2020-01-10T03:25:37.437 回答