// 保存到相册 $("#content").on("click", "#savepic", function () { $("#waitingupload").removeClass("heisebghid").addClass("heisebg"); // 调用html2canvas生成截图 html2canvas($("#orderInfo")[0], { allowTaint: true, taintTest: false, onrendered: function (canvas) { canvas.id = "mycanvas"; //var newImg = document.createElement("img"); //newImg.crossOrigin = "*"; //var ctx = canvas.getContext("2d"); //ctx.drawImage(newImg, 0, 0); ////生成base64图片数据 //var dataUrl = canvas.toDataURL(); //newImg.src = dataUrl; //document.body.appendChild(newImg); var dataUrl = canvas.toDataURL("image/png", 1); // 调用接口解码,获取图片 getimgByBase64(dataUrl); } }); });
// Base64生成图片 function getimgByBase64(base64code) { console.log(base64code); $.ajax({ type: "POST", url: configManager.RequstUrl + "api/common/base64decode", async: true, data: { "base64code": base64code } }).done(function (data) { if ("success" != data.state) { console.log(data.message); plus.nativeUI.alert("保存失败!"); return; } // 保存到相册 saveGallery(data.url); }); }
function saveGallery(url) { var dtask = plus.downloader.createDownload(url, {}, function (d, status) { // 下载完成 if (status == 200) { console.log(d.filename); var filepath = plus.io.convertLocalFileSystemURL(d.filename); console.log(filepath); // 保存相册 plus.gallery.save(filepath); plus.nativeUI.alert("保存成功"); // 删除 plus.io.resolveLocalFileSystemURL(filepath, function (entry) { entry.remove(function (e) { }, function (e) { }); }, function (e) { }); } else { plus.nativeUI.alert("Download failed: " + status); } $("#waitingupload").removeClass("heisebg").addClass("heisebghid"); }); //dtask.addEventListener( "statechanged", onStateChanged, false ); dtask.start(); }
html2canvas截图js的介绍及下载地址如下:http://www.oschina.net/p/html2canvas
作者:Tyler Ning
出处:http://www.cnblogs.com/tylerdonet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址williamningdong@gmail.com
联系我,非常感谢。