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

Ios 旋转图像并保存在React Native中,ios,react-native,image-rotation,rn-fetch-blob,Ios,React Native,Image Rotation,Rn Fetch Blob,我将在react–native中旋转图像,我希望获得旋转图像的base64。 我用了几个图书馆 react native image rotate:它在Android上运行良好,但在iOS上我将rct image store://1作为url,因此我尝试使用rn fetch blob获取base64,但它抛出了无法识别该url的错误 react-native-image-resizer:我使用了此选项,但在iOS中响应不好。如果我设置-90,那么旋转-180,如果我设置-180,那么它旋转为-2

我将在react–native中旋转图像,我希望获得旋转图像的base64。 我用了几个图书馆

  • react native image rotate
    :它在Android上运行良好,但在iOS上我将
    rct image store://1
    作为url,因此我尝试使用
    rn fetch blob
    获取base64,但它抛出了无法识别该url的错误

  • react-native-image-resizer
    :我使用了此选项,但在iOS中响应不好。如果我设置-90,那么旋转-180,如果我设置-180,那么它旋转为-270

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


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

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

      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
    是本地图像。

    最后,我找到了答案

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

    尝试使用Expo图像操纵器


    你可以查看旋转后的图像你是什么意思?谢谢你的回答,我以前检查过imagestore,但它已经被弃用了,现在我不能使用它。我检查过了,你是对的。问题可以在这里找到是的,解决方案是什么?问题是公开的。所以现在可能没有解决方案。嗨tuledev,我可以用ImageResizer.createResizedImage获取rct图像存储url图像
      const rotated = await ImageManipulator.manipulateAsync(
        image.uri,
        [{ rotate: -90 }],
        { base64: true }
      );