微信小程序canvas画布图片保存到相册官方授权、自定义授权、保存

关键步骤介绍

wx.getSetting可以获取授权信息。

wx.authorize首次授权时会打开弹框让用户授权,若用户已选择同意或拒绝,后续不会再显示授权弹框。

如果授权信息显示未进行相册授权,则打开自定义弹框(show_auth: true)让用户选择是否自行配置授权。

如果授权信息显示已进行相册授权,则保存canvas为图片并保存到相册。

.js

javascript 复制代码
  download_canvas(e){
    wx.getSetting()
    .then(get_s_res=>{
      wx.authorize({
        scope: 'scope.writePhotosAlbum',
      })
      if(!get_s_res.authSetting['scope.writePhotosAlbum']){
        this.setData({
          show_auth: true
        })
      }else{
        wx.canvasToTempFilePath({
          x: 0,
          y: 0,
          width: this.data.canvas.width/this.data.pixelRatio,
          height: this.data.canvas.height/this.data.pixelRatio,
          destWidth: this.data.canvas.width,
          destHeight: this.data.canvas.height,
          canvas: this.data.canvas
        })
        .then(c_res=>{
          wx.saveImageToPhotosAlbum({
            filePath: c_res.tempFilePath,
          })
          .then(s_res=>{
            wx.showToast({
              title: '保存到相册成功',
              icon: 'success',
              duration: 2000
            })
          })
          .catch(s_res=>{
            console.log('error',s_res)
          })
        })
        .catch(c_res=>{
          console.log('error',c_res)
        })
      }
    })
    .catch(g_s_res=>{
      console.log('error',g_s_res)
    })

Component实现自定义授权弹框

在component定义授权确认弹框,点击确认,打开settings界面让用户设置授权信息。

.wxml

title和content显示内容由调用主体传入。

html 复制代码
<view class="modal-mask" wx:if="{{show}}">
  <view class="modal" wx:if="{{show}}">
    <view class="info">
      <label class="title">{{title}}</label>
      <text class="content">{{content}}</text>
    </view>   
    <view class="op-button">
      <button size="mini" bind:tap="cancel_and_close" style="box-shadow: 0 0 5rpx darkgray;">取消</button>
      <button size="mini" bind:tap="open_setting" type="primary">确认</button>
    </view>
  </view>
</view>

wxss:

modal-mask实现遮罩效果。

css 复制代码
.modal-mask{
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0,0,0,0.5);
  z-index: 999;
}
.modal{
  position: fixed;
  top: 40%;
  left:15%;
  width: 70%;
  height: 20%;
  background-color: white;
  box-shadow: 0 0 5rpx darkgray;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  align-items: center;
  border-radius: 30rpx;
  z-index: 1000;
}
.info{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: 10rpx
}
.title{
  font-weight: bold;
  white-space:pre-wrap;
  word-wrap:break-word;
  margin-bottom: 10rpx;
}
.content{
  white-space:pre-wrap;
  word-wrap:break-word;
}
.op-button{
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-bottom: 10rpx;
}

.js

wx.openSetting需要通过点击按钮调用,不可直接调用。

javascript 复制代码
// components/show-modal/show-modal.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    show:{
      type:Boolean,
      value:false
    },
    title:{
      type:String,
      value:""
    },
    content:{
      type:String,
      value:""
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    cancel_and_close(){
      this.setData({
        show: false
      })
    },
    open_setting(){
      this.setData({
        show: false
      })
      wx.openSetting()
    }
  }
})

在主体调用component:

.wxml

html 复制代码
<show-modal show="{{show_auth}}" title="警告" content="未完成相册授权,无法保存到相册,请完成授权后继续。"></show-modal>

点击确认,打开settings让用户自行配置授权:

更多微信小程序内容,欢迎关注、评论、私信博主。

相关推荐
只要微微辣11 小时前
Uniapp 微信小程序 Canvas画框标注:拖拽缩放全攻略
前端·微信小程序·uni-app·canvas·canva可画
爱学习的程序媛11 小时前
微信小程序3D开发框架技术对比:XR-Frame与threejs-miniprogram
3d·微信小程序·小程序·前端框架
小羊Yveesss11 小时前
2026年微信小程序开发教程
微信小程序·小程序·notepad++
HYCS1 天前
用pixi.js实现fabric.js(四):StaticCanvas
前端·javascript·canvas
咖啡八杯2 天前
微信小程序人脸认证1.0迁移2.0
后端·微信小程序
xshirleyl2 天前
微信小程序开发week8-慕尚花坊项目
微信小程序·小程序
admin and root2 天前
Claude+Trae大模型 配置Chrome MCP联动Yakit自动化渗透测试
微信小程序·渗透测试·自动化·攻防演练·ai安全·claude code·ai自动化渗透测试
狼丶宇先森2 天前
vue-sign-canvas v2 重构复盘:从 Vue 2 签名板到 Vue 3 + TypeScript 组件库
前端·vue.js·重构·typescript·开源软件·canvas
i查拉图斯特拉如是3 天前
使用workbuddy 30分钟搭建微信小程序
微信小程序·小程序
IceSugarJJ3 天前
Open-AutoGLM项目学习
语言模型·微信小程序·github