我正在使用html2canvas来创建应用程序部分的屏幕截图。我确实有工作代码,但它会提示用户保存。我希望png在没有提示的情况下保存。
这是我的工作代码(但有用户提示):
html2canvas( $('#renderCover'), {
onrendered: function(canvas) {
var base64ToByteArray = function(encStr){
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var decOut = new air.ByteArray();
var bits;
for(var i = 0, j = 0; i<encStr.length; i += 4, j += 3){
bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
decOut[j+0] = ((bits & 0xff0000) >> 16);
if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 2) != 61){
decOut[j+1] = ((bits & 0xff00) >> 8);
}
if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 1) != 61){
decOut[j+2] = (bits & 0xff);
}
}
return decOut;
};
var dataURL = canvas.toDataURL("image/png");
dataURL = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
dataURL = base64ToByteArray(dataURL);
var file = air.File.documentsDirectory.resolvePath("Air Test/testPNG.png");
file.save(dataURL);
}
});
这是我的尝试没有提示,但我收到错误,类型强制失败:
html2canvas( $('#renderCover'), {
onrendered: function(canvas) {
var base64ToByteArray = function(encStr){
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var decOut = new air.ByteArray();
var bits;
for(var i = 0, j = 0; i<encStr.length; i += 4, j += 3){
bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
decOut[j+0] = ((bits & 0xff0000) >> 16);
if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 2) != 61){
decOut[j+1] = ((bits & 0xff00) >> 8);
}
if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 1) != 61){
decOut[j+2] = (bits & 0xff);
}
}
return decOut;
};
var dataURL = canvas.toDataURL("image/png");
dataURL = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
dataURL = base64ToByteArray(dataURL);
var storage = 'documentsDirectory';
var localFile = air.File[storage].resolvePath( "Air Test/testPNG.png" );
var stream = new air.URLStream();
stream.addEventListener(air.Event.COMPLETE, function(e){
var fileData = new air.ByteArray();
stream.readBytes(fileData,0,stream.bytesAvailable);
var fileStream = new air.FileStream();
fileStream.openAsync(localFile, air.FileMode.WRITE);
fileStream.writeBytes(fileData,0);
fileStream.close();
});
stream.load(dataURL);
}
});
有没有办法在没有用户提示的情况下将画布保存为png本地?
答案 0 :(得分:0)
谢谢,但我自己修好了。以下是在没有用户提示的情况下将画布保存为本地.png的工作代码:
html2canvas( $('#renderCover'), {
onrendered: function(canvas) {
var base64ToByteArray = function(encStr){
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var decOut = new air.ByteArray();
var bits;
for(var i = 0, j = 0; i<encStr.length; i += 4, j += 3){
bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
decOut[j+0] = ((bits & 0xff0000) >> 16);
if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 2) != 61){
decOut[j+1] = ((bits & 0xff00) >> 8);
}
if(i+4 != encStr.length || encStr.charCodeAt(encStr.length - 1) != 61){
decOut[j+2] = (bits & 0xff);
}
}
return decOut;
};
var dataURL = canvas.toDataURL("image/png");
dataURL = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
dataURL = base64ToByteArray(dataURL);
var storage = 'documentsDirectory';
var localFile = air.File[storage].resolvePath( "Air Test/testPNG.png" );
var stream = new air.FileStream();
stream.open(localFile, air.FileMode.WRITE);
stream.writeBytes(dataURL,0);
stream.close();
}
});