uni-app - - - - - 保存图片到本地

保存图片到本地

直接上代码:

	export default {
    
    
		data() {
    
    
			return {
    
    
				filePath: "https://s3.pstatp.com/toutiao/static/img/logo.201f80d.png", // 需要下载的图片地址
			};
		},
		methods: {
    
    
			saveImageToPhotosAlbum() {
    
    
				uni.downloadFile({
    
    
					url: this.filePath,
					header: {
    
    
						"content-type": "application/json",
					},
					success: (res) => {
    
    
						let filePath = res.tempFilePath;
						uni.saveImageToPhotosAlbum({
    
    
							filePath, // 暂不支持网络图片/本地图片地址,需与.downloadFile一起使用
							success: (res) => {
    
    
								uni.showToast({
    
    
									title: "成功保存到本地相册"
								});
							},
							fail: (err) => {
    
    
								let errType = err.errMsg.includes(
										"saveImageToPhotosAlbum:fail cancel"
									) ?
									"取消保存" :
									"保存失败";
								uni.showModal({
    
    
									title: errType,
									content: err.errMsg,
									showCancel: false,
								});
							},
						});
					},
					fail: (err) => {
    
    
						uni.showModal({
    
    
							title: "下载出错",
							content: err.errMsg,
							showCancel: false,
						});
					},
				});
			},
}

猜你喜欢

转载自blog.csdn.net/Dark_programmer/article/details/130104686