在做微信小程序的时候需要将图片保存到相册,但是用微信提供的api wx.saveImageToPhotosAlbum 保存更本没有效果,网络图片和本地图片都试过了。下面是我的代码:
代码一:
wx.getImageInfo({
src: '../images/shareimg.jpg',
success: function (res) {
console.log(res.path)
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
wx.saveImageToPhotosAlbum({
filePath: res.path,
success(result) {
console.log(result)
}
})
}
})
}
}
})
}
})
代码二:
wx.downloadFile({
url: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png',
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(result) {
console.log(result)
}
})
wx.saveFile({
tempFilePath: res.tempFilePath,
success: function (res) {
console.log(res.savedFilePath)
}
})
}
})
试了下, 是可以的, 保存之前先
download
一下, 把返回的tempFilePath
临时文件路径给wx.saveImageToPhotosAlbum
的filePath
参数