微信小程序 保存网络照片

供自己使用,大佬文章链接:https://www.jianshu.com/p/0d2011c54f10

// 保存图片

  uploadImage(){

    let _this = this

    wx.getSetting({

        success: (res) => {

            if(res.authSetting['scope.writePhotosAlbum']){

              _this.reiveIamge()

            }else if(res.authSetting['scope.writePhotosAlbum'] === undefined){

                wx.authorize({

                    scope: 'scope.writePhotosAlbum',

                    success() {

                      _this.reiveIamge()

                    },

                    fail(){

                         wx.showToast({

                            title: '您没有授权,无法保存到相册',

                            icon: 'none'

                        })

                    }

                })

            }else{

                wx.openSetting({

                    success(res) {

                        if (res.authSetting['scope.writePhotosAlbum']) {

                          _this.reiveIamge()

                        }else{

                            wx.showToast({

                                title:'您没有授权,无法保存到相册',

                                icon:'none'

                            })

                        }

                    }

                })

            }

        }

    })

  },

  reiveIamge(){

    let that = this

    wx.downloadFile({  //下载图片到本地

      url: that.data.url,

      success (res) {

        if (res.statusCode === 200) {

            let img = res.tempFilePath

            wx.saveImageToPhotosAlbum({  //只支持本地图片所以要先把图片下载下来

                filePath: img,


                   success(result) {

                   wx.showToast({

                       title:'已保存至相册',

                       icon:"success",

                   })

                },fail(res) {

                    wx.showToast({

                       title:'保存失败',

                       icon:"error",

                    })

                }

            })

        }

      }

    })

  },

推荐阅读更多精彩内容