Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export_scale自适应不同宽度的图片 #84

Open
reverendce opened this issue Sep 29, 2021 · 1 comment
Open

export_scale自适应不同宽度的图片 #84

reverendce opened this issue Sep 29, 2021 · 1 comment

Comments

@reverendce
Copy link

reverendce commented Sep 29, 2021

直接修改组件里的pushImg方法就可以自适应不同宽度的图片裁剪生成图

pushImg(src) {
      if (src) {
        this.setData({
          imgSrc: src
        });
        //发现是手动赋值直接返回,交给watch处理
        return;
      }
      // getImageInfo接口传入 src: '' 会导致内存泄漏
      if (!this.data.imgSrc) return;
      wx.getImageInfo({
        src: this.data.imgSrc,
        success: (res) => {
          this.data.imageObject = res;
          // 原图宽高
          this.data.initWidth = res.width; 
          this.data.initHeight = res.height;
          let export_scale = res.width/ this.data.width; //自适应生成图片与裁剪框的比例
          this.setData({
            export_scale: export_scale*0.8, // 同时渲染画布上绘图比例同时压缩成80%大小
          })
          //图片非本地路径需要换成本地路径
          if (this.data.imgSrc.search(/tmp/) == -1) {
            this.setData({
              imgSrc: res.path
            });
          }
          //计算最后图片尺寸
          this._imgComputeSize();
          if (this.data.limit_move) {
            //限制移动,不留空白处理
            this._imgMarginDetectionScale();
          }
          this._draw();
        },
        fail: (err) => {
          this.setData({
            imgSrc: ''
          });
        }
      });
    },
@reverendce
Copy link
Author

reverendce commented Sep 30, 2021

发现一个微信的坑wx.getImageInfo在开发工具和某些机型上获取图片宽高,如果图片的原始信息上方向为right或left,会把宽高的值取反导致绘制的图片直接变形,需要判断一下方向将宽高改回来。

pushImg(src) {
      if (src) {
        this.setData({
          imgSrc: src
        });
        //发现是手动赋值直接返回,交给watch处理
        return;
      }
      // getImageInfo接口传入 src: '' 会导致内存泄漏
      if (!this.data.imgSrc) return;
      wx.getImageInfo({
        src: this.data.imgSrc,
        success: (res) => {
          let width = res.width;
          this.data.imageObject = JSON.parse(JSON.stringify(res)); // 深拷贝防止同时修改了res
          if(res.orientation == 'right' || res.orientation == 'left'){
            // 方向旋转了90度图片信息会把宽高取反,需要把宽和高改回来
           this.data.imageObject.width = res.height;
           width = res.height;
           this.data.imageObject.height = res.width;
          }
          // 原图宽高
          let export_scale = width/this.data.width; //调整生成图片的比例
          if(width > 2000){
            // 如果图片宽度大于2000以上的高清图
            this.setData({
              export_scale: export_scale*0.3, // 同时渲染画布上绘图比例同时压缩成30%大小
            })
          } else {
            this.setData({
              export_scale: export_scale*0.8, // 同时渲染画布上绘图比例同时压缩成80%大小
            })
          }
          //图片非本地路径需要换成本地路径
          if (this.data.imgSrc.search(/tmp/) == -1) {
            this.setData({
              imgSrc: res.path
            });
          }
          //计算最后图片尺寸
          this._imgComputeSize();
          if (this.data.limit_move) {
            //限制移动,不留空白处理
            this._imgMarginDetectionScale();
          }
          this._draw();
        },
        fail: (err) => {
          this.setData({
            imgSrc: ''
          });
        }
      });
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant