首页 > 解决方案 > 如何在本机反应中制作javascript图像blob对象?

问题描述

我必须像这样制作一个图像 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 对象?

标签: javascriptreactjsreact-nativeblob

解决方案


我这样解决了这个问题:

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

推荐阅读