【问题标题】:Rotate image and save in React-Native旋转图像并保存在 React-Native
【发布时间】:2019-12-29 17:35:44
【问题描述】:

我要在 react-native 中旋转图像,我想得到旋转图像的 base64。 我用了几个库

  1. react-native-image-rotate:它在 Android 上运行良好,但在 iOS 上我得到 rct-image-store://1 作为 url,所以我尝试使用 rn-fetch-blob 获取 base64,但它会引发无法识别该 url 的错误。

  2. react-native-image-resizer:我用过这个但是在iOS中反应不好。如果我设置-90然后旋转-180,如果我设置-180那么它旋转为-270。

请帮我解决这个问题,如何在 iOS 中旋转图像。

我需要将图像旋转为 -90、-180、-270、-360(原始)。

【问题讨论】:

  • 可以查看旋转后的图片
  • 什么意思?

标签: ios react-native image-rotation rn-fetch-blob


【解决方案1】:

终于找到答案了。

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);
        },
      );
    }

【讨论】:

    【解决方案2】:

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

      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

    • 那么你可以通过ImageStorereact-native获取base64。
    • 然后用react-native-fs写入本地镜像

    之后你有imagePath 是本地图像。

    【讨论】:

    • 谢谢你的回答,我之前检查了imagestore,但它已经被弃用了,现在我不能使用它了。
    • 我检查了一下,你是对的。这个问题可以在这里找到github.com/itinance/react-native-fs/issues/675
    • 是的,有什么解决办法?
    • 问题已解决。所以目前可能没有解决方案。
    • 嗨 tuledev,我可以使用 ImageResizer.createResizedImage 获取 rct-image-store url 图片
    【解决方案3】:

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

      const rotated = await ImageManipulator.manipulateAsync(
        image.uri,
        [{ rotate: -90 }],
        { base64: true }
      );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2021-06-21
      相关资源
      最近更新 更多