box11 為保存內容的id
saveImg() {
this.$toast.loading({
message: "保存中...",
forbidClick: true,
});
this.createPoster();
},
createPoster() {
const poster = document.getElementById("box11");
html2canvas(poster, {
dpi: window.devicePixelRatio * 2,
backgroundColor: "#fff",
useCORS: true,
scale: 2,
async: true,
}).then((canvas) => {
this.dataImg = canvas.toDataURL("image/png");
console.log(this.dataImg);
this.toImage();
});
},
toImage() {
let filename = "下載二維碼" + ".png";
console.log(window.plus);
if (!window.plus) {
this.$toast("請長按圖片保存到本地");
return;
}
console.log("保存");
let bitmap = new plus.nativeObj.Bitmap();
bitmap.loadBase64Data(this.dataImg);
bitmap.save(
"_doc/" + filename,
{
check: true, // 設置為檢測白屏
overwrite: true,
quality: 100,
},
function(i) {
//保存到系統相冊
plus.gallery.save(
i.target,
function(d) {
//銷毀Bitmap圖片
bitmap.clear();
plus.nativeUI.alert("保存圖片到相冊成功");
},
function(e) {
//銷毀Bitmap圖片
bitmap.clear();
plus.nativeUI.alert("保存到系統相冊失敗,請嘗試重新保存");
}
);
},
function() {
plus.nativeUI.alert("保存失敗,請嘗試重新保存");
}
);
},