原生js,文件流变成图片以前写过,今天在微信小程序中也遇到类似的问题

 

// 形参 arrayBuffer 传入的就是后端返回的文件流
arrayBufferToImage: function (arrayBuffer) {
    const fileSystem = wx.getFileSystemManager();
    const filePath = wx.env.USER_DATA_PATH + '/tempImage' + Date.now() + '.png'; // 临时文件路径
    // 写入文件
    fileSystem.writeFile({
      filePath: filePath,
      data: arrayBuffer, // 直接使用字节流 一定要写
      encoding: 'binary',
      success: (res) => {
        this.setData({
          imagePath: filePath
        });
         wx.setStorageSync('imagePath', filePath)
      },
      fail: (err) => {
        console.error('文件保存失败:', err);
      }
    });
  },

 处理后会变成这种

在页面上使用

 <image class="qrcode {{isShow?'':'qrcode_small'}}" src="{{imagePath}}" mode="widthFix"></image>

 

posted on 2024-11-21 18:47  菜鸟成长日记lx  阅读(411)  评论(0)    收藏  举报