function imageCompress(file) {
return new Promise((resolve, reject) => {
let {
size,
path
} = file
let type = ''
let quality = 80
//大于1M进行压缩,
if (size < (1024 * 1024 * 1.2)) {
resolve(file)
return false
} else {
if (size > (1024 * 1024 * 10)) {
quality = 15
}
if (size > (1024 * 1024 * 2.5)) {
quality = 40
}
}
uni.compressImage({
src: path,
quality: quality,
success: res => {
let newPath = res.tempFilePath
let newName = res.tempFilePath.split("/")[res.tempFilePath.split("/").length - 1]
uni.getFileInfo({
filePath: res.tempFilePath,
success: async (info) => {
let newFile = {
size: info.size,
path: newPath,
name: newName,
tempFilePath: res.tempFilePath
}
resolve(await imageCompress(newFile))
}
})
}
})
})
}
uniapp 压缩图片
最新推荐文章于 2024-04-17 09:38:17 发布