小程序实现长按保存图片

首先是图片展示

      <image :src='invitationMainGraph' @longtap="bankImg" mode='widthFix' class='image'></image>

长按事件:@longtap="bankImg"

事件处理:保存,查看权限,打开权限

    // 保存到本地
    bankImg() {
      console.log(123, "==123===");
      if (this.canSaveAlbum == 0) {
        this.openSetting();
      } else {
        let that = this;
        console.log("11");
        uni.getSetting({
          success(res) {
            console.log(res.authSetting["scope.writePhotosAlbum"]);
            if (!res.authSetting["scope.writePhotosAlbum"]) {
              // 没有权限写入相册
              uni.authorize({
                scope: "scope.writePhotosAlbum",
                success() {
                  console.log("有权限");
                  that.saveImage();
                },
                fail() {
                  console.log("调用权限失败");
                  uni.showToast({
                    title: "保存相册权限未打开,请点击打开设置",
                    icon: "none",
                    duration: 2000,
                    complete() {
                      if ((that.canSaveAlbum = 0)) {
                        this.openSetting();
                      }
                      that.canSaveAlbum = 0;
                    },
                  });
                },
              });
            } else {
              console.log("有");
              // 有权限写入相册
              that.saveImage();
            }
          },
        });
      }
    },
    // 打开权限
    openSetting() {
      console.log("111");
      uni.openSetting({
        success(settingdata) {
          if (settingdata.authSetting["scope.writePhotosAlbum"]) {
            this.canSaveAlbum = 1;
            this.saveImage();
          } else {
            uni.showToast({
              title: "请允许使用保存图片权限",
              icon: "none",
            });
          }
        },
      });
    },
    // 保存海报
    saveImage() {
      uni.showLoading({
        title: "保存中...",
        mask: true,
      });
      uni.downloadFile({
        url: this.invitationMainGraph,
        success: function (res) {
          if (res.statusCode === 200) {
            let img = res.tempFilePath;
            uni.saveImageToPhotosAlbum({
              filePath: img,
              success(res) {
                uni.showToast({
                  title: "保存成功",
                  icon: "success",
                  duration: 2000,
                  complete() {
                    this.show = false;
                  },
                });
              },
              fail(res) {
                wx.showToast({
                  title: "保存失败",
                  icon: "none",
                  duration: 2000,
                });
              },
            });
          }
        },
      });
    },

还有移动端H5是自带长按保存的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值