旋转图像并保存在React-Native中

问题描述 投票:1回答:1

我将以react-native方式旋转图像,我想获得旋转图像的base64。我用了几个库

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

  2. react-native-image-resizer:我使用了此功能,但在iOS中响应不佳。如果我设置-90然后旋转-180,如果我设置-180然后旋转为-270。

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

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

ios react-native image-rotation rn-fetch-blob
1个回答
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中通过react-native获得base64。
  • 然后用react-native-fs将其写入本地图像>
  • 之后,您拥有imagePath是本地图像。

© www.soinside.com 2019 - 2024. All rights reserved.