网络编程
位置:首页>> 网络编程>> JavaScript>> 详解微信小程序图片地扯转base64解决方案

详解微信小程序图片地扯转base64解决方案

作者:dounine  发布时间:2024-06-18 05:52:55 

标签:小程序,图片地扯,base64

你还在找在小程序成如何将图片转base64存储起来并显示嘛,在这里呢,来瞧瞧。

使用方法

js文件


let $this = this;
request({
  url:'https://www.dounine.com/hello.jpg',
  method:'GET',
  responseType: 'arraybuffer',
  success:function(res){
   let base64 = wx.arrayBufferToBase64(res);
   $this.data.userImageBase64 = 'data:image/jpg;base64,' + base64;;
  }
});

wxml文件


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

PS:小程序本地图片转base64最简单方法

  • wx.chooseImage:得到图片地址

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

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


 wx.chooseImage({
  success: function(res) {
   console.log(wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64"))
  },
 })

附官方api:
https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.getFileSystemManager.html?search-key=getFileSystemManager

来源:https://blog.csdn.net/dounine/article/details/79629271

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com