wxml代碼:oop
<view class='erweima'>this
<view class='img_erweima'>url
<image src='http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg' bindtouchstart='timestart' bindtouchend='timeend' bindtap='saveImg'></image>xml
</view>圖片
<view class='text'>資源
<view>長按保存</view>開發
</view>get
</view>it
js代碼:io
//點擊開始時的時間
timestart: function (e) {
var _this = this;
_this.setData({ timestart: e.timeStamp });
},
//點擊結束的時間
timeend: function (e) {
var _this = this;
_this.setData({ timeend: e.timeStamp });
},
//保存圖片
saveImg: function (e) {
var _this = this;
var times = _this.data.timeend - _this.data.timestart;
if (times > 300) {
console.log("長按");
wx.getSetting({
success: function (res) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: function (res) {
console.log("受權成功");
var imgUrl = 「http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg」;//圖片地址
wx.downloadFile({//下載文件資源到本地,客戶端直接發起一個 HTTP GET 請求,返回文件的本地臨時路徑
url: imgUrl,
success: function (res) {
console.log(res);
// 下載成功後再保存到本地
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,//返回的臨時文件路徑,下載後的文件會存儲到一個臨時文件
success: function(res){
}
})
}
})
}
})
}
})
}
},
另外,若是不是處於開發者,而是簡單的練習,不用獲取用戶的當前設置,進行用戶受權。直接--------
//保存圖片
saveImg: function (e) {
var _this = this;
var times = _this.data.timeend - _this.data.timestart;
if (times > 300) {
console.log("長按");
var imgUrl = 「http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg」;//圖片地址
wx.downloadFile({//下載文件資源到本地,客戶端直接發起一個 HTTP GET 請求,返回文件的本地臨時路徑
url: imgUrl,
success: function (res) {
console.log(res);
// 下載成功後再保存到本地
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,//返回的臨時文件路徑,下載後的文件會存儲到一個臨時文件
success: function(res){
}
})
}
}
},