微信小程序开发 js 长按保存图片
新版本微信客户端是不需要另外写代码的,以下代码主要是为兼容:<image src="{{url}}" data-url="{{url}}" show-menu-by-longpress="true" bindlongpress="saveImage"></image>
// 长按保存功能--授权部分
saveImage (e) {
let _this = this
wx.showActionSheet({
itemList: ['保存到相册'],
success(res) {
let url = e.currentTarget.dataset.url;
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
// 同意授权
_this.saveImgInner(url);
},
fail: (res) => {
console.log(res);
wx.showModal({
title: '保存失败',
content: '请开启访问手机相册权限',
success(res) {
wx.openSetting()
}
})
}
})
} else {
// 已经授权了
_this.saveImgInner(url);
}
},
fail: (res) => {
console.log(res);
}
})
},
fail(res) {
console.log(res.errMsg)
}
})
},
// 长按保存功能--保存部分
saveImgInner (url) {
wx.getImageInfo({
src: url,
success: (res) => {
let path = res.path;
wx.saveImageToPhotosAlbum({
filePath: path,
success: (res) => {
console.log(res);
wx.showToast({
title: '已保存到相册',
})
},
fail: (res) => {
console.log(res);
}
})
},
fail: (res) => {
console.log(res);
}
})
},