Blogger Information
Blog 56
fans 7
comment 11
visits 222236
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
小程序保存图片到相册
樂成的开发笔记
Original
1460 people have browsed it

开发过程中需要保存到相册,可以使用这个方法,要注意的是需要配置downloadFile合法域名

实例

 // 保存到相册
 savePoster() {
  let that = this
  wx.getSetting({
    success(res) {
      if (res.authSetting['scope.writePhotosAlbum']) {
        that.saveImg();
      } else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
        wx.authorize({
          scope: 'scope.writePhotosAlbum',
          success() {
            that.saveImg();
          },
          fail() {
            that.authConfirm()
          }
        })
      } else {
        that.authConfirm()
      }
    }
  })
},
// 授权拒绝后,再次授权提示弹窗
authConfirm() {
  let that = this
  wx.showModal({
    content: '检测到您没打开保存图片权限,是否去设置打开?',
    confirmText: "确认",
    cancelText: "取消",
    success: function (res) {
      if (res.confirm) {
        wx.openSetting({
          success(res) {
            if (res.authSetting['scope.writePhotosAlbum']) {
              that.saveImg();
            } else {
              wx.showToast({
                title: '您没有授权,无法保存到相册',
                icon: 'none'
              })
            }
          }
        })
      } else {
        wx.showToast({
          title: '您没有授权,无法保存到相册',
          icon: 'none'
        })
      }
    }
  });
},
//保存图片
saveImg: function () {
  var that = this;
  var imgUrl = that.data.imgurl; //需要保存的图片地址
  console.log(imgUrl)
  wx.downloadFile({
    url: imgUrl,
    success(res) {
      // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
      console.log('downloadFileres', res);
      wx.saveImageToPhotosAlbum({
        filePath: res.tempFilePath,
        success(res) {
          console.log("保存图片:success");
          wx.showToast({
            title: '保存成功',
          });
        },
        fail: res => {
          console.log(res);
          wx.showToast({
            title: '保存失败',
          });
        }
      })
    }
  })
},

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!