如何实现微信小程序图片转base64编码的详细步骤解析?
- 内容介绍
- 文章标签
- 相关推荐
本文共计335个文字,预计阅读时间需要2分钟。
你还在寻找如何在小程序中将图片转换为base64存储并显示的方法吗?在这里,我为你提供了一个简洁的解决方案。直接使用JavaScript文件,通过以下代码实现:
javascriptlet $this=this;request({ url: 'http://img.558idc.com/uploadfile/allimg/js/hello.jpg', method: 'GET', responseType: 'arraybuffer'}).then(response=> { let base64=btoa(String.fromCharCode.apply(null, new Uint8Array(response))); // 在这里处理base64字符串,例如存储或显示});
快来试试吧!
你还在找在小程序成如何将图片转base64存储起来并显示嘛,在这里呢,来瞧瞧。
使用方法
js文件
let $this = this; request({ url:'img.558idc.com/uploadfile/allimg/js/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:
developers.weixin.qq.com/miniprogram/dev/api/file/wx.getFileSystemManager.html?search-key=getFileSystemManager
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计335个文字,预计阅读时间需要2分钟。
你还在寻找如何在小程序中将图片转换为base64存储并显示的方法吗?在这里,我为你提供了一个简洁的解决方案。直接使用JavaScript文件,通过以下代码实现:
javascriptlet $this=this;request({ url: 'http://img.558idc.com/uploadfile/allimg/js/hello.jpg', method: 'GET', responseType: 'arraybuffer'}).then(response=> { let base64=btoa(String.fromCharCode.apply(null, new Uint8Array(response))); // 在这里处理base64字符串,例如存储或显示});
快来试试吧!
你还在找在小程序成如何将图片转base64存储起来并显示嘛,在这里呢,来瞧瞧。
使用方法
js文件
let $this = this; request({ url:'img.558idc.com/uploadfile/allimg/js/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:
developers.weixin.qq.com/miniprogram/dev/api/file/wx.getFileSystemManager.html?search-key=getFileSystemManager
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

