微信小程序base64图片转换临时链接

561 阅读1分钟

小程序内新建 base64src.js

const base64src = (base64data, fun) => {
  const base64 = base64data; //base64格式图片
  const time = new Date().getTime();
  const imgPath = wx.env.USER_DATA_PATH + "/poster" + time + "share" + ".png";
  //如果图片字符串不含要清空的前缀,可以不执行下行代码.
  const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
  const file = wx.getFileSystemManager();
  file.writeFileSync(imgPath, imageData, "base64");
  fun(imgPath);
};

export { base64src };

业务脚本内使用

import { base64src } from "../../utils/base64src"; // 后面引用路径为base64src.js文件路径


const url = 'base64图片路径';
base64src(url, (res) => {
	console.log(res); // 转换后的临时连接路径
});

简单记录如有问题或更优解还请不要吝啬的共同沟通学习,此结。