【问题标题】:React Native - CameraRoll - Access base64 version of imageReact Native - CameraRoll - 访问 base64 版本的图像
【发布时间】:2019-11-12 16:31:31
【问题描述】:

我正在使用 RN 相机胶卷 (https://facebook.github.io/react-native/docs/cameraroll.html) 访问一系列图像以作为表单的一部分进行选择。我的问题是 - 有谁知道是否可以访问图像的 base64 - 或者在这种情况下最简单的转换方法。

这是我的 usePhotos 类:

 usePhotos = () => {
  CameraRoll.getPhotos({
      first: 20,
      assetType: 'Photos',
    })
    .then(r => {
      this.setState({ ADModalState:false,
        ADMediaCamera:false, isModalVisible:true, ADphotos: r.edges, ADPhotosBase:r.edges });
    })
    .catch((err) => {
       //Error Loading Images
       console.warn(err);
    });
  };

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    我认为 RN Camera Form 不可能,但一年前我用作解决方案的是这个 npm 包:https://www.npmjs.com/package/react-native-image-base64 它仍然下载了很多,所以我认为它仍在运行

    【讨论】:

    • 谢谢@Uber - 看起来会很完美 - 但我在运行 pod install 时收到此错误 - [!] RNImgToBase64 pod 由于 1 个错误而无法验证: - 错误 |属性:缺少必需的属性homepage。 - 警告 |来源:版本应包含在 Git 标记中。 - 警告 | github_sources:Github 存储库应以.git 结尾。
    • @Dancer 我会从旧壁橱里拿出我的 Mac 试试看会回复你的
    • @Dancer 我为它制作了一个简单的 podfile!将此文件放在 node_modules a.uguu.se/Im0GbOqKSv8b_RNImgToBase64.podspec 链接内 react-native-image-base64 文件夹的根目录中,因此只能使用 24 小时,所以要快 ;)
    • 啊-为您的帮助/努力欢呼@Uber-但我现在才收到此错误:[!]在../node_modules/react-native-image-base64 中找不到RNImgToBase64 的podspec。 - 它对你有用吗?
    • 为您的所有帮助干杯 @Uber - 仍然无法安装 pod - 随机停止 ――― TEMPLATE END ―――――――――――――――――― - - - - - - - - - - - - - - - - - - - - - - - [!] 哦不,发生了错误。 .. - 可能会与另一个图书馆一起寻找 B 计划.. 干杯
    【解决方案2】:

    这是我最终的解决方法:

    if(Platform.OS === 'ios'){
          if (uriPhoto.startsWith('ph://'))
          {
            imagePATH = uriPhoto.substring(5,41);
            let photoPATH = `assets-library://asset/asset.JPG?id=${imagePATH}&ext=JPG`;
    
            const dest = `${RNFS.TemporaryDirectoryPath}${Math.random().toString(36).substring(7)}.jpg`;
    
            RNFS.copyAssetsFileIOS(photoPATH, dest, 500, 500, 1.0, 1.0, 'contain')
              .then(data => {
                RNFS.readFile(data, 'base64')
                  .then(base64 => {
    
                    this.state.ADImageArray.push(uriPhoto);
                    this.props.encodedImage(base64);
    
                    //TODO - Delete file
                  });
              });
          }
        }
        else if(Platform.OS === 'android'){
    
          RNFS.readFile(uriPhoto, 'base64')
            .then(base64 => {
              this.state.ADImageArray.push(uriPhoto);
              this.props.encodedImage(base64);
            });
        }
    

    【讨论】:

      猜你喜欢
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2023-03-30
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多