帖子
帖子
用户
博客
课程
显示全部楼层
15
帖子
0
勋章
92
Y币
发表于 2018-7-7 06:56:20
5
未解决

trans和fs模块问题,求救 [复制链接]

  • 发表于:2018-07-07 06:56:20
Ctrl+O 能保存也能读取,如果是Ctrl+I或者是编译后就不能读取图片了,有大神能帮帮忙吗?具体代码如下:
  1. function domToPic() {
  2.             var wrapper =$(".info");
  3.             var trans = api.require('trans');
  4.             var fs = api.require('fs');
  5.             var wx = api.require('wx');
  6.             var w = $(".info").width();
  7.             var h = $(".info").height();
  8.             var canvas = document.createElement('canvas');
  9.             var context = canvas.getContext('2d');
  10.             var getPixelRatio = function(context) {
  11.                 var backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1;
  12.                 return (window.devicePixelRatio || 1) / backingStore;
  13.             };
  14.             var ratio = getPixelRatio(context);
  15.             canvas.width = w * ratio;
  16.             canvas.height = h * ratio;
  17.             canvas.style.width = w + 'px';
  18.             canvas.style.height = h + 'px';
  19.             context.scale(ratio, ratio);
  20.             html2canvas(wrapper, {
  21.                 canvas : canvas,
  22.                 background : '#fff',
  23.                 allowTaint : true
  24.             }).then(function(canvas) {
  25.                 var imgData = canvas.toDataURL('image/png');
  26.                 imgData = imgData.replace('data:image/png;base64,', '');
  27.                 trans.saveImage({
  28.                     base64Str : imgData,
  29.                     album :true,
  30.                     imgPath:"fs://img",
  31.                     imgName : 'shares001.png',
  32.                 }, function(ret, err) {
  33.                     alert(JSON.stringify(ret)+'asdfas') //返回true
  34.                     fs.getAttribute({
  35.                         path: 'fs://img/shares001.png'
  36.                     }, function(ret, err) {
  37.                         alert(JSON.stringify(ret)+'12111221'); //返回 false
  38.                         if (ret.status) {

  39.                         } else {
  40.                             alert( JSON.stringify( err )+'adfzcc123123' );  //返回文件或文件夹不存在
  41.                         }
  42.                     });
  43.                 });
  44.             });
  45.         }
复制代码


13
帖子
6
勋章
105
Y币
你没alert一下在哪出的问题吗
6
帖子
7
勋章
198
Y币
function drwImg() {
        html2canvas(document.querySelector("#canvas-box")).then(function(canvas) {
            // document.body.appendChild(canvas)
            var image = new Image();
            image.src = canvas.toDataURL("image/webp", 1);
            var base64Str = image.src.split('base64,')[1];
            var imgPath = "fs://qrcode/";
            var imgName = Math.floor((Math.random() * 100) + 1) + "share.jpg";
            var trans = api.require('trans');
            trans.saveImage({
                base64Str: base64Str, //base64字符串不能包含data:image/png;base64,前缀
                album: false,
                imgPath: imgPath,
                imgName: imgName
            }, function(ret, err) {
                if (ret.status) {
                    iosImages[0] = imgPath + imgName;
                    $api.dom($api.domAll('.flex-list')[0], 'img').src = api.fsDir + "/qrcode/" + imgName;
                    // alert(JSON.stringify(ret));
                } else {
                    // alert(JSON.stringify(err));
                }
            });
        });
    }


这是我曾经写过的一部分代码!有些CSS不兼容,建议你将生成的图片加载到html页面上,看看是否生成成功!还有个生成的图片质量应该会有影响,把图片质量调低试试!

15
帖子
0
勋章
92
Y币
maokun00 发表于 2018-7-7 08:28
你没alert一下在哪出的问题吗
  1.   trans.saveImage({
  2.                     base64Str : imgData,
  3.                     album :true,
  4.                     imgPath:"fs://img",
  5.                     imgName : 'shares001.png',
  6.                 }, function(ret, err) {
  7.                     alert(JSON.stringify(ret)+'asdfas') //返回true
  8.                     fs.getAttribute({
  9.                         path: 'fs://img/shares001.png'
  10.                     }, function(ret, err) {
  11.                         alert(JSON.stringify(ret)+'12111221'); //返回 false
  12.                         if (ret.status) {

  13.                         } else {
  14.                             alert( JSON.stringify( err )+'adfzcc123123' );  //返回文件或文件夹不存在
  15.                         }
  16.                     });
  17.                 });
复制代码




这不是有吗
15
帖子
0
勋章
92
Y币
初级码农 发表于 2018-7-7 10:00
function drwImg() {
        html2canvas(document.querySelector("#canvas-box")).then(function(canvas) ...

把图片的质量调低了也是一样的,不为何 我appendChild dom上没有图片
6
帖子
7
勋章
198
Y币
Taurusly 发表于 2018-7-7 13:08
把图片的质量调低了也是一样的,不为何 我appendChild dom上没有图片

那就是生成图片都有问题咯。
您需要登录后才可以回帖 登录

本版积分规则