微信小程序图片与base64互转

 微信小程序中图片转base64

wx.getFileSystemManager().readFile({
    filePath: imgUrl,
    encoding: "base64",
    success: res => {
        //返回base64格式
        var base64Str = 'data:image/png' + ';base64,' + res.data
    },
    fail: err => {
        console.log(err)
    }
})

wx.getFileSystemManager().readFile 描述

微信小程序中base64转图片

const base64 = base64; //base64格式图片
const time = new Date().getTime();
//USER_DATA_PATH:文件系统中的用户目录路径 (本地路径)
const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "" + ".png";
const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
const file = wx.getFileSystemManager();
file.writeFileSync(imgPath, imageData, "base64");
console.log(imgPath);

//imgPath就是图片在本地的地址
//如需保存至本地

 wx.saveImageToPhotosAlbum({
       filePath: imgPath,
        success: (res) => {
            wx.showModal({
                title: '照片已保存至相册',
                content: '快去分享给小伙伴吧',
                confirmText: '我知道了',
                showCancel: false,
           })
       }
 })

 writeFileSync描述

猜你喜欢

转载自blog.csdn.net/qq_42044542/article/details/131088188