
图片流转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
大家在看
关注公众号: 页面仔小杨 【实战干货、原创分享】

本文介绍了微信小程序中如何将图片流转为base64格式进行展示。通过wx.chooseImage获取本地图片,利用wx.getFileSystemManager读取文件并转化为base64。同时,文章还提及了读者可能感兴趣的面试技巧、小程序开发经验及本地服务搭建。
714

被折叠的 条评论
为什么被折叠?



