uni-app下载线上图片保存到相册

直接上代码

/* 
    1、需要生成一个小程序码 或者 后端帮忙处理返回一个图片地址
    2、将其下载到本地
    3、将链接变为对象 执行下载
*/
视图页面
<view  class="btnShareImage" @click.stop="saveImage()">保存分享二维码</view>
// js methods
            // 请求生成二维码并 下载
			saveImage () {
				let that = this;
				uni.showLoading({
					title: '正在保存中'
				})
				let data = {
					uid: that.uid,
					merId: that.merId,
					deptId: that.deptId,
					id: that.Info.id
				}
				getGiftImageCode(data).then(res=> {
					
					this.posterImage = res.data.url;
					console.log(this.posterImage);
					// this.savePosterPath()
					this.saveNetImageToLocal(res.data.url);
					uni.hideLoading();
				}).catch(err=> {
					console.log(err);
					uni.hideLoading();
				})
				
			},
			saveNetImageToLocal(url){
				let that = this
				uni.downloadFile({
					url:url,
					success:(res)=>{
						if (res.statusCode === 200) {
							that.posterImage = res.tempFilePath; //地址
							that.savePosterPath();
						} else {
							uni.showToast({
								title: '网络错误',
							}); 
						}
					}
				});
			},
			/*
			 * 保存到手机相册
			 */
			// #ifdef MP
			savePosterPath: function() {
				let that = this;
				uni.getSetting({
					success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									uni.saveImageToPhotosAlbum({
										filePath: that.posterImage,
										success: function(res) {
											that.$util.Tips({
												title: '保存成功',
												icon: 'success'
											});
										},
										fail: function(res) {
											console.log(res)
											that.$util.Tips({
												title: '保存失败'
											});
										}
									})
								}
							})
						} else {
							uni.saveImageToPhotosAlbum({
								filePath: that.posterImage,
								success: function(res) {
									that.$util.Tips({
										title: '保存成功',
										icon: 'success'
									});
								},
								fail: function(res) {
									console.log(res);
									that.$util.Tips({
										title: '保存失败'
									});
								},
							})
						}
					}
				})
			},
			// #endif
			// #ifdef APP-PLUS
			savePosterPath(){
				let that = this
				uni.saveImageToPhotosAlbum({
					filePath: that.posterImage,
					success: function(res) {
						that.$util.Tips({
							title: '保存成功',
							icon: 'success'
						});
					},
					fail: function(res) {
						that.$util.Tips({
							title: '保存失败'
						});
					},
				})
			}
			// #endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值