我正在尝试将图像保存为encoded64,然后从本地存储中获取图像的值。但是为什么我得到空值?
这是我的 fiddle : http://jsfiddle.net/sAH8w/7/
function getBase64Image(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to
// guess the original format, but be aware the using "image/jpg"
// will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
try {
localStorage.setItem("elephant", dataURL);
}
catch (e) {
alert('error')
console.log("Storage failed: " + e);
}
//return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
最佳答案
这个小改动解决了问题:
$('#save').click(function(){
var image = new Image();
image.src = "https://dl.dropboxusercontent.com/s/t2ywui846zp58ye/plus_minus_icons.png?m=";
getBase64Image(image);
})
工作 fiddle :http://jsfiddle.net/robertrozas/sAH8w/15/
更新(获取按钮):http://jsfiddle.net/robertrozas/sAH8w/17/
关于javascript - 为什么保存值是 "null"(尝试以encode64保存图像)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24388957/