compressImg() {
return new Promise(resolve => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d')
const imgList = this.imgList;
let newImgList = []
for (let i = 0; i < imgList.length; i++) {
let img = new Image()
img.src = imgList[i];
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.clearRect(0, 0, img.width, img.height)
ctx.drawImage(img, 0, 0, img.width, img.height)
const base64 = canvas.toDataURL("image/jpeg", 0.7);
newImgList.push(base64)
if (newImgList.length === 3) resolve(newImgList)
}
}
})
}
```javascript
//将完整的base64转换为blob
dataURLtoBlob (base64) {
var arr = base64.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: mime })
},
canvas.toBlob(blob => {
let fileObject = new File([blob], "cover.img", {
type: "image/jpeg"
})
console.log('f:', fileObject)
}, "image/jpeg", 1)
const reader = new FileReader();
reader.onload = function(evt) {
console.log(evt);
};
reader.readAsArrayBuffer(res.tempFilePath)