|
|
首先,在WXML文件中为Image组件添加bindlongtap属性绑定长按事件,例如:<image src="{{imageUrl}}" bindlongtap="saveImage" />
JS:
- Page({
- data: {
- imageUrl: '图片路径' // 可以是网络或本地路径
- },
- saveImage: function(e) {
- const url = e.currentTarget.dataset.src || this.data.imageUrl;
- const that = this;
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.writePhotosAlbum']) {
- wx.authorize({
- scope: 'scope.writePhotosAlbum',
- success() {
- that.saveImageToAlbum(url);
- },
- fail() {
- wx.showModal({
- title: '提示',
- content: '需要开启相册权限才能保存图片',
- success(res) {
- if (res.confirm) {
- wx.openSetting();
- }
- }
- });
- }
- });
- } else {
- that.saveImageToAlbum(url);
- }
- }
- });
- },
- saveImageToAlbum: function(url) {
- const that = this;
- wx.getImageInfo({
- src: url,
- success(res) {
- wx.saveImageToPhotosAlbum({
- filePath: res.path,
- success() {
- wx.showToast({ title: '保存成功', icon: 'success' });
- },
- fail() {
- wx.showToast({ title: '保存失败', icon: 'none' });
- }
- });
- },
- fail() {
- wx.showToast({ title: '图片加载失败', icon: 'none' });
- }
- });
- }
- });
复制代码
|
|