微信小程序保存图片的方法

 下载(wx.downloadFile) + 保存(wx.saveImageToPhotosAlbum) +授权(wx.openSetting)


    // 保存图片的方法
    serve(img) {
        wx.showLoading({
            title: '加载中...'
        });
        //wx.downloadFile方法:下载文件资源到本地
        wx.downloadFile({
            url: img, //图片地址
            success: function (res) {
                //wx.saveImageToPhotosAlbum方法:保存图片到系统相册
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath, //图片文件路径
                    success: function (data) {
                        wx.hideLoading(); //隐藏 loading 提示框
                        wx.showToast({
                            title: '保存成功',
                            icon: 'none'
                        })
                    },
                    // 接口调用失败的回调函数
                    fail: function (err) {
                        if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
                            wx.showModal({
                                title: '提示',
                                content: '请授权保存到相册',
                                modalType: false,
                                success: res => {
                                    if (res.confirm) {
                                        wx.openSetting({
                                            success(settingdata) {
                                                console.log("settingdata", settingdata)
                                                if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                                    wx.showToast({
                                                        title: '授权成功',
                                                        icon: 'none'
                                                    })
                                                } else {
                                                    wx.showToast({
                                                        title: '授权失败',
                                                        icon: 'none'
                                                    })
                                                }
                                            },
                                            fail(failData) {
                                                console.log("failData", failData)
                                            },
                                            complete(finishData) {
                                                console.log("finishData", finishData)
                                            }
                                        })
                                    } else if (res.cancel) {
                                        console.log('用户点击取消')
                                    }
                                }
                            })
                        }
                    },
                    complete(res) {
                        wx.hideLoading(); //隐藏 loading 提示框
                    }
                })
            }
        })
    },

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值