var img = new Image();
img.src = imageurl;
// img.setAttribute("crossOrigin", 'Anonymous');
img.onload = function() {
var that = this;
// 默认按比例压缩
var w = that.width,
h = that.height,
scale = w / h;
w = (obj.width || w) * 0.5;
h = (obj.height || (w / scale));
var quality = 0.92; // 默认图片质量为0.92
//生成canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 创建属性节点
var anw = document.createAttribute("width");
anw.nodeValue = w;
var anh = document.createAttribute("height");
anh.nodeValue = h;
canvas.setAttributeNode(anw);
canvas.setAttributeNode(anh);
ctx.drawImage(that, 0, 0, w, h);
// quality值越小,所绘制出的图像越模糊
console.log('关键点');
var base64 = canvas.toDataURL('image/jpeg', quality);
callback(base64);
- 发布:2019-12-06 17:11
- 更新:2020-07-17 15:22
- 阅读:1721
因iOS13起UIWebview被列为非公开API,未来会被App Store拒绝。从HBuilderX 2.3.0+起,iOS App的默认webview从UIwebview改为了WKWebview。WKWebview与UIWebview有部分区别,包括更严格的联网或canvas跨域限制,详见:https://ask.dcloud.net.cn/article/36348
pxy - pxy
我跟你遇到同样的问题了,你在把图片放进canvas之前先转换成base64就可以了
-
回复 XiaoGuaiSss: function PNGtoBase64(url) {
return new Promise(function(resolve, reject){
var bitmap = new plus.nativeObj.Bitmap("test");
// 从本地加载Bitmap图片
bitmap.load(url, function() {
var data = bitmap.toBase64Data()
// console.error(data)
console.error('图片转换成功')
resolve(data)
}, function(e) {
console.log('图片转换失败:' + JSON.stringify(e));
});
})
}
就是你本地的图片在往canvas里加载之前先用这个方法把图片转成base 64再进行之前的逻辑就不会报错了
2019-12-24 10:50
-
base64: function(data) {
var img = document.getElementById("im");
var canvas = document.createElement("canvas");
//是在这里放这段逻辑吗?
canvas.height = 300;
canvas.width = 300;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, data.x, data.y, data.width, data.height, 0, 0, 300, 300);
var dataURL = canvas.toDataURL("image/jpeg", 1.0); //第二个参数是质量
return dataURL;
}
2020-05-30 11:43
守护
5+app 不能使用https://ext.dcloud.net.cn/plugin?id=123这个插件,如何改写canvas.toDataURL方法?
2020-02-20 12:24