ITPub博客

首页 > IT职业 > IT职场 > 微信小程序图片流&本地图片转base64处理方案

微信小程序图片流&本地图片转base64处理方案

IT职场 作者:castlight 时间:2021-09-09 08:51:19 0 删除 编辑

图片描述

图片流转base64展示


const that = this;
request({
   url: 'XXXXX',
   method: 'GET',
   responseType: 'arraybuffer',
   success: function(res) {
     const base64 = wx.arrayBufferToBase64(res);
     that.setData({
       userImageBase64: `data:image/jpg;base64,${base64}`
     });
   }
}); 

wxml展示图片


image  src='{{userImageBase64}}'  style='width: 100rpx; height: 100rpx;' />

本地图片转base64


wx.chooseImage({
  success (res) {
    // tempFilePath可以作为img标签的src属性显示图片
    const tempFilePaths = res.tempFilePaths;
    const fileManager = wx.getFileSystemManager();
    const base64 = fileManager.readFileSync(tempFilePaths[0], 'base64');
    console.log('=============================', base64);
  },
  fail () {
    wx.showToast({
      title: '获取图片失败',
      icon: 'success',
      duration: 2000
    })
  }
})

  • wx.chooseImage:获取本地图片

  • wx.getFileSystemManager:创建文件管理类

  • readFileSync:读取本地文件,直接得到base64

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1470/viewspace-2807028/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    102
  • 访问量
    1241