微信小程序保存图片/保存base64图片
保存普通图片
// 获取访问相册权限
function getAuthorize() {
let that = this
//判断用户是否授权"保存到相册"
wx.getSetting({
success(res) {
//没有权限,发起授权
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() { //用户允许授权,保存图片到相册
that.saveImage();
},
fail() { //用户点击拒绝授权,跳转到设置页,引导用户授权
wx.openSetting({
success() {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.saveImage();
}
})
}
})
}
})
} else { //用户已授权,保存到相册
that.saveImage()
}
}
})
}
//保存图片到相册,提示保存成功
function saveImage() {
let that = this
wx.downloadFile({
url: that.wxaCode,
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '保存成功',
icon: "success",
duration: 1000
})
}
})
}
})
}
保存base64图片
// 获取访问相册权限
function getAuthorize() {
let that = this
//判断用户是否授权"保存到相册"
wx.getSetting({
success(res) {
//没有权限,发起授权
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() { //用户允许授权,保存图片到相册
that.saveImage();
},
fail() { //用户点击拒绝授权,跳转到设置页,引导用户授权
wx.openSetting({
success() {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.saveImage();
}
})
}
})
}
})
} else { //用户已授权,保存到相册
that.saveImage()
}
}
})
}
//保存图片到相册,提示保存成功
function saveImage() {
let that = this
wx.downloadFile({
url: that.wxaCode,
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '保存成功',
icon: "success",
duration: 1000
})
}
})
}
})
}