base64 图片计算大小 bytes

  1. const base64toBlob = (dataURI: string) => {
  2. try {
  3. const matches = dataURI.match(/data:(.+);/);
  4. if (!matches || matches.length < 1) return null;
  5. const imgType = matches[1];
  6. const byteString = atob(dataURI.split(',')[1]);
  7. const ab = new ArrayBuffer(byteString.length);
  8. const ia = new Uint8Array(ab);
  9. for (let i = 0; i < byteString.length; i++) {
  10. ia[i] = byteString.charCodeAt(i);
  11. }
  12. return new Blob([ab], { type: imgType });
  13. } catch (e) {
  14. return null;
  15. }
  16. };