小程序生成海报并保存到相册

小程序生成海报并保存到相册


* 图片必须是本地的不能是base64位的

1.安装

# npm 
npm i wxa-plugin-canvas -S --production 
 # yarn 
 yarn add wxa-plugin-canvas --production

2.配置

"usingComponents": {
    "poster": "wxa-plugin-canvas/poster"
 },

3.引入

import Poster from 'wxa-plugin-canvas/poster/poster'

4.使用

4.1:wxss文件

 <view class="handel_poster_btn btn" bindtap="drawSharePic">
    <!-- 生成海报的按钮 -->
    <image src="../../image/invite/poster_btn.png"></image>
</view>

<poster id="poster" config="{{posterConfig}}" bind:success="onPosterSuccess" bind:fail="onPosterFail"></poster>
<view wx:if="{{showposterImg}}" class="popup-mask"></view>
 <!-- 透明黑的背景 -->
    <view wx:if="{{showposterImg}}" class="posterImg-box">
          <image mode="widthFix" class="posterImg"               src="{{posterImg}}"></image>
  <view class="btn-create" bindtap="savePosterPic">保存到相册的阿牛    </view>
  <view class="close_img">
     <!-- 关闭透明层的按钮图片 -->
    <image src="../../image/invite/close.png" class="img" bindtap="closePoster"></image>
  </view>
</view>

4.2:js文件

 drawSharePicDone(qrcode, pic) {
    this.setData({
      posterConfig: {
        width: 620,
        height: 954,
        debug: false,
        images: [{
            x: 0,
            y: 0,
            url: pic, // 背景图片
            width: 620,
            height: 954
          },
          {
            x: 150,
            y: 500,
            url: qrcode, // 二维码
            width: 320,
            height: 320
          }
        ],
      }
    }, () => {
      Poster.create();
    });
  },
  savePosterPic() {
    const _this = this
    wx.saveImageToPhotosAlbum({
      filePath: this.data.posterImg,
      success: (res) => {
        console.log('保存成功')
        wx.showModal({
          content: '已保存到手机相册',
          showCancel: false,
          confirmText: '知道了',
          confirmColor: '#333'
        })
      },
      complete: () => {
        _this.setData({
          showposterImg: false
        })
      },
      fail: (res) => {
        if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny" || res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
          console.log("当初用户拒绝,再次发起授权")
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: modalSuccess => {
              wx.openSetting({
                success(settingdata) {
                  console.log("settingdata", settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限成功,再次点击图片即可保存',
                      showCancel: false,
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限失败,将无法保存到相册哦~',
                      showCancel: false,
                    })
                  }
                },
                fail(failData) {
                  console.log("failData", failData)
                },
                complete(finishData) {
                  console.log("finishData", finishData)
                }
              })
            }
          })
        }
      }
    })
  },
  closePoster: function () {
    this.setData({
      showposterImg: false
    })
  },

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值