收藏
回答

canvas 组件内使用canvasToTempFilePath

框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本
小程序 Bug canvasToTempFilePath 客户端 Android 6.6.7 1.9.1
const testImg="../../assets/img/01.jpg"
Component({
  properties: {
  },
  data: {
    canvas:{
      width:'',
      height:''
    }
  },
  ready(){
    this._init();
  },
  methods: {
    _init(){
      let _this=this;
      let { windowWidth, windowHeight } = wx.getSystemInfoSync();
      wx.getImageInfo({
        src: testImg,
        success(res){
          if (res.errMsg === "getImageInfo:ok"){
            let {width,height}=res;
            console.log(`开始图片=>width${res.width},height${res.height}`);
            let $width = windowWidth
            let _radio = $width /width;
            let $height = height*_radio;
            _this.setData({
              canvas:{
                width: $width,
                height:$height
              }
            });
            _this._drawImg();
          }
        }
      })
    },
    _drawImg(){
      let _this=this;
      let { width,height}=_this.data.canvas;
      const ctx = wx.createCanvasContext('myCanvas', _this);
      ctx.drawImage(testImg, 0, 0, width, height);
      ctx.draw(true,()=>{
        wx.canvasToTempFilePath(
          {
            canvasId: 'myCanvas',
            success: function (res) {
              if (res.errMsg == 'canvasToTempFilePath:ok') {
                wx.getImageInfo({
                  src: res.tempFilePath,
                  success(res) {
                    console.log(`压缩之后=>width${res.width},height${res.height}`);//图片不完整 大约比canvas正常 1/3
                  }
                });
              }
            },
            fail(err) {
            }
          },
          _this
        );
      });
    }
  }
});


最后一次编辑于  2018-07-01
回答关注问题邀请回答
收藏

2 个回答

  • hexford~🐶
    hexford~🐶
    2018-07-02

    麻烦给个相关的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html),我们定位下问题

    2018-07-02
    有用
    回复 1
  • river
    river
    2018-07-01

    刚没有描述明确  是在component 中使用canvas 组件  canvas 的宽高 都是正确的

    wx.canvasToTempFilePath(

    {

    canvasId: 'myCanvas',

    success(res){

        //导出的图片之后1/3 左右    

        //开发工具上边正常

        //手机是小米mix2

        //不知道大家遇到过没

    }

    },this

    2018-07-01
    有用
    回复
登录 后发表内容