- 发布:2020-05-23 09:58
- 更新:2020-08-14 15:32
- 阅读:1161
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: win7
HBuilderX类型: 正式
HBuilderX版本号: 2.7.5
手机系统: 全部
页面类型: vue
打包方式: 云端
项目创建方式: HBuilderX
测试过的手机:
示例代码:
saveWatermarkImageToPhotosAlbum: function(tempFilePath) {
uni.getImageInfo({
src: tempFilePath,
success: function(image) {
let canvasId = 'canvas';
let canvas = uni.createCanvasContext(canvasId)
_self.canvasW = image.width;
_self.canvasH = image.height;
let width = parseInt(image.width)
let height = parseInt(image.height)
uni.showLoading({
title: "正在绘制图片",
mask: true
})
canvas.drawImage(tempFilePath, 0, 0, width, height)
uni.showLoading({
title: "正在绘制水印",
mask: true
})
//水印
let wWidth = width * 0.2;
let hHeight = height * 0.1;
let girardLeft = width * 0.78;
//款号
canvas.setFillStyle("#FFFFFF")
let fontSize = width / 1000 * 50;
let font = "italic bolder " + fontSize.toString() + "px 宋体 sans-serif";
canvas.font = font;
//宽度不为一千时,款号不显示
canvas.fillText("123AL", girardLeft, height - 30)
console.log("draw");
uni.showLoading({
title: "正在绘制图片",
mask: true
})
canvas.draw(false,
function(eee) {
uni.showLoading({
title: "正在导出图片",
mask: true
})
console.log("canvasToTempFilePath start");
uni.canvasToTempFilePath({
x: 0,
y: 0,
width: width,
height: height,
destWidth: width,
destHeight: height,
canvasId: canvasId,
success: function(canvasRes) {
// 在H5平台下,tempFilePath 为 base64
console.log("canvasToTempFilePath success");
uni.saveImageToPhotosAlbum({
filePath: canvasRes.tempFilePath,
success: function() {
console.log("saveImageToPhotosAlbum success");
},
fail: function(e) {
console.log("saveImageToPhotosAlbum fail");
}
});
},
fail: function(canvasRes) {
console.log("canvasToTempFilePath fail: " + JSON.stringify(canvasRes));
}
})
}
);
}
})
}
saveWatermarkImageToPhotosAlbum: function(tempFilePath) {
uni.getImageInfo({
src: tempFilePath,
success: function(image) {
let canvasId = 'canvas';
let canvas = uni.createCanvasContext(canvasId)
_self.canvasW = image.width;
_self.canvasH = image.height;
let width = parseInt(image.width)
let height = parseInt(image.height)
uni.showLoading({
title: "正在绘制图片",
mask: true
})
canvas.drawImage(tempFilePath, 0, 0, width, height)
uni.showLoading({
title: "正在绘制水印",
mask: true
})
//水印
let wWidth = width * 0.2;
let hHeight = height * 0.1;
let girardLeft = width * 0.78;
//款号
canvas.setFillStyle("#FFFFFF")
let fontSize = width / 1000 * 50;
let font = "italic bolder " + fontSize.toString() + "px 宋体 sans-serif";
canvas.font = font;
//宽度不为一千时,款号不显示
canvas.fillText("123AL", girardLeft, height - 30)
console.log("draw");
uni.showLoading({
title: "正在绘制图片",
mask: true
})
canvas.draw(false,
function(eee) {
uni.showLoading({
title: "正在导出图片",
mask: true
})
console.log("canvasToTempFilePath start");
uni.canvasToTempFilePath({
x: 0,
y: 0,
width: width,
height: height,
destWidth: width,
destHeight: height,
canvasId: canvasId,
success: function(canvasRes) {
// 在H5平台下,tempFilePath 为 base64
console.log("canvasToTempFilePath success");
uni.saveImageToPhotosAlbum({
filePath: canvasRes.tempFilePath,
success: function() {
console.log("saveImageToPhotosAlbum success");
},
fail: function(e) {
console.log("saveImageToPhotosAlbum fail");
}
});
},
fail: function(canvasRes) {
console.log("canvasToTempFilePath fail: " + JSON.stringify(canvasRes));
}
})
}
);
}
})
}
操作步骤:
调用函数
调用函数
预期结果:
调用api花费的时间过长
调用api花费的时间过长
实际结果:
以下为荣耀note10的部分运行日志,从日志打印canvasToTempFilePath start 到 canvasToTempFilePath success 的时间大概为12S
09:40:43.284 draw at pages/search_detail/search_detail.vue:487
09:40:43.824 canvasToTempFilePath start at pages/search_detail/search_detail.vue:498
09:40:55.044 canvasToTempFilePath success at pages/search_detail/search_detail.vue:509
09:40:55.364 saveImageToPhotosAlbum success at pages/search_detail/search_detail.vue:514
09:40:56.364 draw at pages/search_detail/search_detail.vue:487
09:40:56.844 canvasToTempFilePath start at pages/search_detail/search_detail.vue:498
09:41:08.604 canvasToTempFilePath success at pages/search_detail/search_detail.vue:509
09:41:08.924 saveImageToPhotosAlbum success at pages/search_detail/search_detail.vue:514
以下为荣耀note10的部分运行日志,从日志打印canvasToTempFilePath start 到 canvasToTempFilePath success 的时间大概为12S
09:40:43.284 draw at pages/search_detail/search_detail.vue:487
09:40:43.824 canvasToTempFilePath start at pages/search_detail/search_detail.vue:498
09:40:55.044 canvasToTempFilePath success at pages/search_detail/search_detail.vue:509
09:40:55.364 saveImageToPhotosAlbum success at pages/search_detail/search_detail.vue:514
09:40:56.364 draw at pages/search_detail/search_detail.vue:487
09:40:56.844 canvasToTempFilePath start at pages/search_detail/search_detail.vue:498
09:41:08.604 canvasToTempFilePath success at pages/search_detail/search_detail.vue:509
09:41:08.924 saveImageToPhotosAlbum success at pages/search_detail/search_detail.vue:514
bug描述:
从开始调用api到执行success回调的速度相较之前的版本所花费的时间大幅度增加,HBuilderX2.6.16版的时候基本只要花费0.5~1秒钟左右。更新到2.7.5之后安卓花费的时间变成了10秒左右,ios花费的时间为2~3秒左右;
efficient_work
牛逼,有用
2022-05-04 19:14