微信小程序保存图片的两种方法

小程序 0

wx.showShareImageMenu( )

功能描述:打开分享图片弹窗,可以将图片发送给朋友、收藏或下载

效果如下:

 //下载wx.downloadFile({   url: 'https://res.wx.qq.com/wxdoc/dist/assets/img/demo.ef5c5bef.jpg',   success: (res) => {     wx.showShareImageMenu({       path: res.tempFilePath //要分享的图片地址,必须为本地路径或临时路径     })   } })

如果只想要把图片保存到系统相册,不想要打开分享弹窗,可以使用:

wx.saveImageToPhotosAlbum( )

功能描述:保存图片到系统相册

// 点击下载图片事件downloadImg(url) {	let that = this	wx.showLoading({		title: '加载中...'	});	//wx.downloadFile方法:下载文件资源到本地	wx.downloadFile({		url: url, //图片地址		success: function(res) {			//wx.saveImageToPhotosAlbum方法:保存图片到系统相册			wx.saveImageToPhotosAlbum({				filePath: res.tempFilePath, //图片文件路径				success: function(data) {					wx.hideLoading(); //隐藏 loading 提示框					wx.showModal({						title: '提示',						content: '保存成功',						modalType: false,						complete() {							that.closePicturePopup()							that.closeSharePopup()						}					})							},				// 接口调用失败的回调函数				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: '提示',							content: '需要您授权保存相册',							modalType: false,							success: modalSuccess => {								wx.openSetting({									success(settingdata) {										console.log("settingdata",											settingdata)										if (settingdata											.authSetting[												'scope.writePhotosAlbum'											]) {											wx.showModal({												title: '提示',												content: '获取权限成功,再次点击图片即可保存',												modalType: false,											})										} else {											wx.showModal({												title: '提示',												content: '获取权限失败,将无法保存到相册哦~',												modalType: false,											})										}									},									fail(failData) {										console.log("failData",											failData)									},									complete(finishData) {										console.log("finishData",											finishData)									}								})							}						})					}				},				complete(res) {					wx.hideLoading(); //隐藏 loading 提示框				}			})		}	})}

也许您对下面的内容还感兴趣: