微信小程序editor上传图片转化为base64格式

方法:通过官网提供的api实现

关键代码

const imgUrl = 'data:image/png;base64,' + wx.arrayBufferToBase64(wx.getFileSystemManager().readFileSync(tempFilePaths));

完整代码加详细解释

//此事件为点击图片按钮上传的事件
insertImage() {
    const that = this
//关于API  wx.chooseImage解释见官网
//https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html
    wx.chooseImage({
      count: 1,//一次只能选择一张图片
      sizeType: ['original', 'compressed'],//原图或者是压缩图
      sourceType: ['album', 'camera'],//相册或者是相机现拍
      success: function (res) {
        const tempFilePaths = res.tempFilePaths[0];//图片文件
        //划重点了,此处为关键代码,结合百度经验和官方文档得来
//https://jingyan.baidu.com/article/cb5d61055c2b36405c2fe0aa.html
//https://developers.weixin.qq.com/miniprogram/dev/api/base/wx.arrayBufferToBase64.html
//https://developers.weixin.qq.com/miniprogram/dev/api/file/FileSystemManager.readFileSync.html
        const imgUrl = 'data:image/png;base64,' + wx.arrayBufferToBase64(wx.getFileSystemManager().readFileSync(tempFilePaths));
        //editorCtx为编辑器
        //更多editor的解释见官网
//https://developers.weixin.qq.com/miniprogram/dev/api/media/editor/EditorContext.html
//https://developers.weixin.qq.com/miniprogram/dev/api/media/editor/EditorContext.insertImage.html
        that.editorCtx.insertImage({
          src: imgUrl,
          success: function () {
            console.log('insert image success')
          }
        })
      }
    })
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值