uniapp 获取二进制流转换为图片并分享至微信

获取二进制流

 uni.request({
     url: 'xxx',
     responseType: 'arraybuffer',
     header: {
       'Authorization':  store.getItem(constant.USER_INFO).token || ''
     },
     success: (res) => {
         const arrayBuffer = res.data
         this.imgurl = 'data:image/jpeg;base64,'+ uni.arrayBufferToBase64(arrayBuffer)
         this.downLoadImg()
       }
     });

将base64图片保存本地并分享微信

downLoadImg() {
      var bitmap = new plus.nativeObj.Bitmap();
      bitmap.loadBase64Data(
        this.imageUrl,
        function () {
          const url = "_doc/" + new Date().getTime() + ".png";
          bitmap.save(
            url,
            {
              overwrite: true,
              quality: "quality",
            },
            function (i) {
              uni.hideLoading();
              uni.share({
                provider: "weixin",
                scene: "WXSceneSession",
                type: 2,
                imageUrl: i.target,
                success: function (res) {
                  uni.showToast({
                    title: "图片分享成功",
                    icon: "none",
                    duration: 2000,
                  });
                },
                fail: function (err) {
                  console.log("fail:" + JSON.stringify(err));
                },
              });
              console.log("保存图片成功:" + JSON.stringify(i));
            },
            function (e) {
              console.log("保存图片失败:" + JSON.stringify(e));
            }
          );
        },
        function (e) {
          console.log("绘制图片失败:" + JSON.stringify(e));
        }
      );
    },

推荐阅读更多精彩内容