功能描述:保存小程序base64图片
应用场景:小程序
适用范围:微信平台
保存base64类型图片
var imgSrc = '图片的编码';//base64编码
var save = wx.getFileSystemManager();
var number = Math.random();
save.writeFile({
filePath: wx.env.USER_DATA_PATH + '/pic' + number + '.png',
data: imgSrc.slice(22),
encoding: 'base64',
success: res => {
wx.saveImageToPhotosAlbum({
filePath: wx.env.USER_DATA_PATH + '/pic' +number+'.png',
success: function (res) {
wx.showToast({
title: '保存成功',
})
},
fail: function (err) {
console.log(err)
}
})
console.log(res)
}, fail: err => {
console.log(err)
}
})
保存远程链接形式的图片
var a = this;
wx.saveImageToPhotosAlbum ? (wx.showLoading({
title: "正在保存图片...",
mask: !1
}), wx.downloadFile({
url: '填写一个远程的图片链接',
success: function (t) {
wx.showLoading({
title: "正在保存图片",
mask: !1
}), wx.saveImageToPhotosAlbum({
filePath: t.tempFilePath,
success: function () {
wx.showModal({
title: "自定义提示信息",
content: "保存成功",
showCancel: !1
});
},
fail: function (t) {
wx.showModal({
title: "图片保存失败",
content: t.errMsg,
showCancel: !1
});
},
complete: function (t) {
wx.hideLoading();
}
});
},
fail: function (t) {
wx.showModal({
title: "图片下载失败",
content: t.errMsg,
showCancel: !1
});
},
complete: function (t) {
wx.hideLoading();
}
})) : wx.showModal({
title: "自定义提示信息",
content: "该版本过低,无法使用该功能!",
showCancel: !1
});