涉及到的api
uni.createCanvasContext(canvasId, this); 画布api
getCurrentPages(); 获取当前页面实例api
page.$getAppWebview().draw
截屏绘制 将Webview窗口的可视区域截屏并绘制到Bitmap图片对象中。
screenshot(canvasId, clip = {}) {
// #ifdef APP-PLUS
const ctx = uni.createCanvasContext(canvasId, this);
var pages = getCurrentPages(); //获取当前页面信息
var page = pages[pages.length - 1];
var bitmap = null;
var currentWebview = page.$getAppWebview();
bitmap = new plus.nativeObj.Bitmap('amway_img');
let that = this
// 将webview内容绘制到Bitmap对象中
currentWebview.draw(bitmap, () => {
// console.log('截屏绘制图片成功');
let rand = Math.floor(Math.random() * 10000)
let saveUrl = '_doc/' + rand + 'share.jpg';
bitmap.save(saveUrl, {}, (i) => {
uni.saveImageToPhotosAlbum({
filePath: i.target,
success: function() {
uni.showToast({
title: '保存图片成功',
mask: false,
duration: 1500
});
}
})
})
}, (err) => {
console.log(err)
}, {
clip
})
// #endif
},