【发布时间】:2015-02-14 11:37:52
【问题描述】:
我在我的 chrome 应用程序上工作,内容安全政策令人不安。我想从 json (我不知道的字符串/对象/数组) 中获取 jpeg 图像,但是 csp 每次都阻止了我。现在我知道使用数据链接更容易,所以I searched a converter。
function getdt(img) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, img.width, img.height);
// Get the data-URL formatted image.
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
//Example
getdt('http://ms01.oe3.fm/oe3metafiles/Pictures/200/929203.22.jpg');
不幸的是,这不起作用。错误信息是这样的:
Uncaught TypeError:
Failed to execute 'drawImage' on 'CanvasRenderingContext2D':
No function was found that matched the signature provided.
我希望有人可以帮助我:) 对不起,我的英语不好
【问题讨论】:
-
什么是
img?提供相关代码
标签: jquery canvas jpeg drawimage data-url