zoukankan      html  css  js  c++  java
  • 微信小程序点击保存图片到相册

    wxml部分的代码
     
    <view class="footer-r" bindtap="save">
    <image src="../../../images/icons/zhiwen.svg" />
    <view class="footer-r-t">
    <text>点击保存到相册</text>
    <text class="t">享更多优惠</text>
    </view>
    </view>
    wxsl部分的代码
    .footer-r {
    50%;
    display: flex;
    flex-direction: column;
    }

    .footer-r image {
    margin: 0 auto;
    180rpx;
    height: 180rpx;
    }

    .footer-r-t {
    margin-left: 20rpx;
    font-size: 26rpx;
    padding-top: 20rpx;
    display: flex;
    flex-direction: column;
    text-align: center;
    }
    js部分的代码
    //点击保存图片
    save() {
    let that = this
     
    //若二维码未加载完毕,加个动画提高用户体验
    wx.showToast({
    icon: 'loading',
    title: '正在保存图片',
    duration: 1000
    })
    //判断用户是否授权"保存到相册"
    wx.getSetting({
    success(res) {
    //没有权限,发起授权
    if (!res.authSetting['scope.writePhotosAlbum']) {
    wx.authorize({
    scope: 'scope.writePhotosAlbum',
    success() { //用户允许授权,保存图片到相册
    that.savePhoto();
     
    },
    fail() { //用户点击拒绝授权,跳转到设置页,引导用户授权
    wx.openSetting({
    success() {
    wx.authorize({
    scope: 'scope.writePhotosAlbum',
    success() {
    that.savePhoto();
    }
    })
    }
    })
    }
    })
    } else { //用户已授权,保存到相册
    that.savePhoto()
    }
    }
    })
    },
    //下载图片地址并保存到相册,提示保存成功
    savePhoto() {
    let that = this
    wx.downloadFile({
    // 图片下载地址
    url: 'https://bhb.hvateng.com/public/static/baiheba/img/qrcode344.jpg',
    // url: app.apiUrl.url + 'Userequity/poster',
    success: function(res) {
    wx.saveImageToPhotosAlbum({
    filePath: res.tempFilePath,
    success(res) {
    wx.showToast({
    title: '保存成功',
    icon: "success",
    duration: 1000
    })
    }
    })
    }
    })
    }
  • 相关阅读:
    事务传播机制,搞懂。
    洛谷 P1553 数字反转(升级版) 题解
    洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here 题解
    洛谷 P1055 ISBN号码 题解
    洛谷 P2141 珠心算测验 题解
    洛谷 P1047 校门外的树 题解
    洛谷 P1980 计数问题 题解
    洛谷 P1008 三连击 题解
    HDU 1013 题解
    HDU 1012 题解
  • 原文地址:https://www.cnblogs.com/Annely/p/11185210.html
Copyright © 2011-2022 走看看