图片流转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;' />
<!--more-->
本地图片转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
大家在看
- 面试官:遇到一个从没接触过的问题你是怎么解决的?
- 开发小程序分页性能时的这些坑你遇到过吗?
- live-server本地搭建长期服务