我使用以下代码从URL获取图像并将其转换为Base64:
function convertImgToBase64(url, callback){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img,0,0);
var dataURL = canvas.toDataURL();
callback.call(this, dataURL);
canvas = null;
};
img.src = url;
}
convertImgToBase64(INSER_URL_HERE, function(base64Img){
$("#data_url").text(base64Img);
imageRef.set(base64Img);
});
我很困惑。到目前为止,代码工作和img.onload仅在我使用维基百科的图像(例如http://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png)进行测试时被激活!我尝试过的所有其他外部图片网址都不会触发img.onload(例如http://www.ipadenclosures.com/php-oak/themes/global/admin_images/Apple_gray_logo.png)。任何想法为什么这些图像没有加载?