var image = new Image();image.src = URL;image.onload = function () {var canvas = @R_403_4617@.createElement("canvas");var img_wIDth = this.wIDth;var img_height = this.height;canvas.wIDth =1000;canvas.height = 1000;var w=(1000/img_wIDth);var h=(1000/img_height);var ctx = canvas.getContext("2d");ctx.scale(w,h);ctx.drawImage(this,0); var dataURL = canvas.toDataURL("image/png",1);
这里的问题是,图像宽度设置为画布宽度但高度不是.
此外,该代码在AndroID设备和ipads上完美运行,但问题仅出现在iphone上.
在Iphone 4s和5c上测试过
谁能帮帮我吗!!!!
输入图像是:
画布上的图像输出(作为png文件)是:
解决方法 通过修改以下链接中提供的答案,我能够为我的案例创建一个解决方法,如下所示:When scaling and drawing an image to canvas in iOS Safari,width is correct but height is squished
我稍微调整了一下代码,所以现在看起来像这样:
var image = new Image(); image.src = URL; image.onload = function () { var canvas = @R_403_4617@.createElement("canvas"); var img_wIDth = image.wIDth; var img_height = image.height; canvas.wIDth = 600; canvas.height = 600; var ctx = canvas.getContext("2d"); var imgRatio; if (image.wIDth > image.height) { imgRatio = image.wIDth / image.height; } else { imgRatio = image.height / image.wIDth; } var canRatio = canvas.wIDth / canvas.height; var scaleDWIDth = image.wIDth * (canvas.height / image.height); var scaledHeight; if (platformVersion.substring(0,1) < 8) { scaledHeight = (image.height * (canvas.wIDth / image.height)) * 2.1; } else { scaledHeight = (image.height * (canvas.wIDth / image.height)); } if (imgRatio > canRatio) { ctx.drawImage(image,canvas.wIDth,scaledHeight); } else { ctx.drawImage(image,scaleDWIDth,canvas.height); } var dataURL = canvas.toDataURL("image/png",1); }
这里取决于手机的 *** 作系统是否来自cordova设备插件,我将一个值乘以缩放高度以补偿压缩效果.
总结以上是内存溢出为你收集整理的在cordova ios iphone中转换为base64时图像缩小全部内容,希望文章能够帮你解决在cordova ios iphone中转换为base64时图像缩小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)