wx.showActionSheet:显示操作菜单 ;
wx.downloadFile:下载文件到本地;
wx.saveImageToPhotosAlbum:保存图片到系统相册;
wxml:
<image src="图片地址" catchlongpress="handleLongPress"></image>
js:
Page({
data: {
},
onLoad: function (options) {
},
// 长按保存图片事件
handleLongPress: function handleLongPress() {
var _this1 = this;
wx.showActionSheet({
itemList: ['保存到相册'],
success: function success(res) {
switch (res.tapIndex) {
case 0:
wx.downloadFile({
url: _this1.data.codeImg,
success (res) {
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function success(res) {
wx.showToast({
title: '已保存到相册',
icon: 'success',
duration: 3000
});
},
fail: function fail(err) {
console.log(err);
}
});
}
}
})
break;
default:
break;
}
},
fail: function fail(res) {
console.log(res.errMsg);
}
});
this.setData({
showActionsheet: true
});
},
})