我正在使用Phonegap的相机功能在我的应用程序中保存图片 . 当我尝试通过其保存的file_URI(我从相机获取)获取文件时,图像不会加载 .
function toBase64(url) {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
var img = new Image();
img.src = url;
if ( img.height != 0 ) {
var height = img.height, width = img.width;
canvas.height = height;
canvas.width = width;
ctx.drawImage(img, 0, 0, width, height);
try {
var dataURL = canvas.toDataURL("image/jpg");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
catch (err) { console.log("ERROR " + err);}
}
else {
alert("Wrong path!");
}
}
图像保存在应用程序的缓存文件夹中(/data/data/my.app/cache)
关于问题可能来自何方的任何想法?
1 回答
我修复了这个问题,不得不使用Phonegap的FileReader对象 .
不要认为“while(base64 ==”“){}”虽然是一个很好的做法...
编辑:我使用this gentleman的do_when方法而不是空循环!