【问题标题】:How to make javascript image blob object in react native?如何在本机反应中制作javascript图像blob对象?
【发布时间】:2021-12-26 21:45:00
【问题描述】:

我必须像这样制作一个图像 blob 对象:

现在我可以在我存储在变量中的 react native 中获取图像 base64 url​​:

const base64 = img.data;

我试过这种方法,但没有用:

  function b64toBlob(dataURI) {
      var byteString = atob(dataURI.split(',')[1]);
      var ab = new ArrayBuffer(byteString.length);
      var ia = new Uint8Array(ab);
      for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
      }
      return new Blob([ab], {type: 'image/jpeg'});
    }
    const blob = b64toBlob(base64);

    console.log(blob);

const base64 = img.data;

现在如何在 react native 中使用这个 base64 变量创建一个像上图一样的 blob 对象?

【问题讨论】:

    标签: javascript reactjs react-native blob


    【解决方案1】:

    我通过这种方式解决了这个问题:

      ImagePicker.openPicker({
                          width: 400,
                          height: 400,
                          cropping: true,
                          includeBase64: true,
                        })
                          .then(image => {
                            setImg(image);
                          })
                          .then(() => setModalVisible(prev => !prev));
    
    
      const image = {
            name: nn,
            uri: img.path,
            type: img.mime,
            size: img.size,
            lastModifiedDate: JSON.parse(img.modificationDate),
            uid: img.modificationDate,
          };
          const form = new FormData();
          form.append('file', image);
          axios
            .post('url', form, {
              headers: {
                Authorization: `bearer ${JSON.parse(token)}`,
              },
            })
    

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 2021-09-04
      • 2021-05-24
      • 1970-01-01
      • 2019-02-01
      相关资源
      最近更新 更多