根据下方代码,生成图片时有概率白屏,初步调试后发现和SETTIMEOUT的时间有关(手机内存越小发生几率越高,TIMEOUT300ms时有总体4%的发生率,2000MS时有总体2%的发生率),是否有优化/解决方案?
return new Promise((resolve, reject) => {
var that = this;
uni.getImageInfo({
src: list[index].url,
success: (ress) => {
let maxWidth = 300;
let maxHeight = 300;
let img = ress;
let targetWidth
let targetHeight
if (img.width > maxWidth && img.height > maxHeight) {
const rate = Math.min(maxWidth / img.width, maxHeight / img.height)
targetWidth = img.width rate
targetHeight = img.height rate
} else if (img.width > maxWidth) {
targetWidth = maxWidth
targetHeight = (maxWidth / img.width) img.height
} else if (img.height > maxHeight) {
targetHeight = maxHeight
targetWidth = (maxHeight / img.height) img.width
} else {
targetWidth = img.width
targetHeight = img.height
}
let ctx = uni.createCanvasContext('firstCanvas', that); //创建画布
//将图片src放到cancas内,宽高为图片大小
ctx.drawImage(list[index].url, 0, 0, targetWidth, targetHeight)
ctx.setFontSize(12) //字体大小
ctx.setFillStyle('#ffffff') //字体颜色
let textToWidth = targetWidth * 0.02; //字体宽度
let textToHeight = targetHeight * 0.85; //字体高度
ctx.fillText(that.CurentTime(), textToWidth, textToHeight) // 设置字体
ctx.draw(false, setTimeout(()=>{
uni.canvasToTempFilePath({//将画布中内容转成图片,即水印与图片合成
width: targetWidth, // 画布宽
height: textToHeight, // 画布高
canvasId: 'firstCanvas',
quality: 0.5,
fileType: 'jpg',
success: (res) => {
list[index].url = res.tempFilePath
resolve(true);
},
fail: (e)=> {
uni.showModal({
title: "图片转化失败,原因(" + JSON.stringify(e) + ")",
showCancel: false,
});
},
complete: ()=> {
// console.log("complete");
},
})
},2000))
}
})
})
1 个回复
FullStack - 【插件开发】【专治疑难杂症】【ios上架、马甲包、白包、过审、已成功上架过几百个】【多款插件已上架:https://ext.dcloud.net.cn/publisher?id=22130】【非诚勿扰】QQ:543610866
延迟生成