Taro 微信小程序,下载/保存图片 到本地相册

//也是参考了一大堆网上的资料,做出来的,亲测好用!!!!!

//下载图片

  goDownImg=(e)=>{//下载图片

    e.stopPropagation();

    this.closeOpenSet();

    let {currImg,prevList}=this.state;

    //let imgPath1=prevList[currImg].img_url;

    let imgPath1 ='https://aaa.tech.cn/1.jpg';

    let that =this;

    Taro.getSetting({

success (res) {

if (!res.authSetting['scope.writePhotosAlbum']) {//没有授权

          Taro.authorize({

scope:'scope.writePhotosAlbum',

            success () {

that.downloadImgToAlbum(imgPath1);

            },

            fail(){

that.setState({//显示授权层

                openSetting:true

              })

}

})

}else {//已授权

          that.downloadImgToAlbum(imgPath1);

        }

}

})

}

downloadImgToAlbum (imgPath1) {

Taro.showToast({

title:'正在保存,请稍等',

      icon:'none',

      duration:2000

    })

//下载图片

    this.downloadHttpImg(imgPath1)

.then((res)=>{

this.sharePosteCanvas(res)

})

//生成海报

//this.drawBall(imgPath1);

  }

downloadHttpImg =(httpImg) =>{

return new Promise(((resolve, reject) => {

Taro.downloadFile({

url: httpImg,

        success: (res) => {

if (res.statusCode ===200) {

resolve(res.tempFilePath)

}else {

Taro.showToast({

title:'图片下载失败!',

              icon:'none',

              duration:1000

            })

}

},

        fail: (res) => {

Taro.showToast({

title:'提示图片下载失败!',

            icon:'none',

            duration:1000

          })

}

})

}))

}

sharePosteCanvas =(imgUrl)=> {

Taro.saveImageToPhotosAlbum({

filePath: imgUrl,

      success (res) {

Taro.showToast({

title:'图片已保存到相册',

          icon:'none',

          duration:1000

        })

},

      fail (err) {

Taro.showToast({

title:'图片保存失败',

          icon:'none',

          duration:1000

        })

}

})

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容