二进制流转base64加快速度

updateImgList() {
const reg = /.(w+)$/
const __arrPromise__ = []
const self = this
for (let i = 0; i < self.totalPicture[self.pageNum].length; i++) {
const fn = new Promise((resolve) => {
self.$api.getArrayBuffer(self.totalPicture[self.pageNum][i].id).then((response) => {
let obj = null
// 是否TIF和TIFF格式的图片
if (self.totalPicture[self.pageNum][i].filePath.match(reg)[1] === 'tif' || self.totalPicture[self.pageNum][i].filePath.match(reg)[1] === 'tiff') {
// eslint-disable-next-line no-undef
const tiff = new Tiff({ buffer: response })
obj = {
id: self.totalPicture[self.pageNum][i].id,
type: 'tif',
url: tiff.toDataURL()
}
} else {
obj = {
type: 'mormal',
id: self.totalPicture[self.pageNum][i].id,
url: ('data:data/png;base64,' + btoa(
new Uint8Array(response).reduce((data, byte) => data + String.fromCharCode(byte), '')
))
// '/label/job-picture/show-image?id=' + self.totalPicture[self.pageNum][i].id
}
}
resolve(obj)
}).catch(error => {})
})
__arrPromise__.push(fn)
}
Promise.all(__arrPromise__).then((res) => {
self.imgList = self.imgList.concat(res)
let activeIndex = 0
if (self.imgSwiper) {
activeIndex = self.imgSwiper.activeIndex
}
if (self.pageNum == 0) {
self.$nextTick(() => {
self.initSwiper()
})
}
self.updateTopImg(self.imgList[activeIndex].id)
})
},
原文地址:https://www.cnblogs.com/MDGE/p/12034415.html