当前位置:网站首页>Method of saving pictures in wechat applet

Method of saving pictures in wechat applet

2022-06-26 04:40:00 Residual white x

  download (wx.downloadFile) + preservation (wx.saveImageToPhotosAlbum) + to grant authorization (wx.openSetting)


    //  How to save pictures 
    serve(img) {
        wx.showLoading({
            title: ' Loading ...'
        });
        //wx.downloadFile Method : Download file resources to local 
        wx.downloadFile({
            url: img, // Picture address 
            success: function (res) {
                //wx.saveImageToPhotosAlbum Method : Save picture to system album 
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath, // Image file path 
                    success: function (data) {
                        wx.hideLoading(); // hide  loading  Prompt box 
                        wx.showToast({
                            title: ' Saved successfully ',
                            icon: 'none'
                        })
                    },
                    //  Interface call failed callback function 
                    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: ' Tips ',
                                content: ' Please authorize saving to album ',
                                modalType: false,
                                success: res => {
                                    if (res.confirm) {
                                        wx.openSetting({
                                            success(settingdata) {
                                                console.log("settingdata", settingdata)
                                                if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                                    wx.showToast({
                                                        title: ' Authorized success ',
                                                        icon: 'none'
                                                    })
                                                } else {
                                                    wx.showToast({
                                                        title: ' privilege grant failed ',
                                                        icon: 'none'
                                                    })
                                                }
                                            },
                                            fail(failData) {
                                                console.log("failData", failData)
                                            },
                                            complete(finishData) {
                                                console.log("finishData", finishData)
                                            }
                                        })
                                    } else if (res.cancel) {
                                        console.log(' The user clicks cancel ')
                                    }
                                }
                            })
                        }
                    },
                    complete(res) {
                        wx.hideLoading(); // hide  loading  Prompt box 
                    }
                })
            }
        })
    },

原网站

版权声明
本文为[Residual white x]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260436492675.html