报错如图,无法保存图片到相册
相关代码片段1:点击保存图片按钮,一直报错显示“Uncaught (in promise) save image failed”
onLoad: function (options) { api.showLoading(); console.log(app.globalData.picParam); let {x,y,logoHeight, logoWidth, avatarHeight, avatarWidth, logoUrl, avatarUrl} = app.globalData.picParam; this .setData({ canvasWidth: logoWidth, canvasHeight: logoHeight }); const ctx = wx.createCanvasContext( 'avatarPic' ); ctx.drawImage(logoUrl, 0, 0, logoWidth, logoHeight); ctx.draw(); ctx.save(); // 先保存状态 已便于画完圆再用 ctx.beginPath(); //开始绘制 //先画个圆 ctx.arc(x + avatarWidth / 2, y + avatarHeight / 2 , avatarWidth / 2, 0, Math.PI * 2, false ); ctx.clip(); //画了圆 再剪切 原始画布中剪切任意形状和尺寸。一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内 ctx.drawImage(avatarUrl, x, y, avatarWidth, avatarHeight); ctx.draw( true ); api.hideLoading(); }, downloadPic() { api.saveCanvas( 'avatarPic' ) .then(res => { return api.saveImage(res.tempFilePath); }) .then(res => { api.showToast( '保存成功' , 'success' ); }); }, |
相关代码片段2:点击保存小程序二维码,但点击后仅返回了临时文件路径,没有保存至手机相册中
onSelect(event) { console.log(event.detail.name); let _this = this ; switch (event.detail.name) { case '保存小程序二维码' : wx.cloud.downloadFile({ fileID: 'cloud://cloud-lzj35.636c-cloud-lzj35-1300316134/0.png' , // 文件 ID success: res => { // 返回临时文件路径 let tempPath = res.tempFilePath console.log(tempPath); wx.saveImageToPhotosAlbum({ filePath: tempPath, success(res) { // const savedFilePath = res.savedFilePath; api.showToast( '保存成功' , 'success' ); console.log(res); _this.onCancel() } }) }, fail: console.error }) break ; default : _this.onCancel() break ; } }, |
saveImg: function(){
wx.downloadFile({
url:'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
success:function(res){
let path = res.tempFilePath
wx.saveImageToPhotosAlbum({
filePath: path,
success(res) {
console.log(res)
},
fail(res) {
console.log(res)
},
complete(res) {
console.log(res)
}
})
},fail:function(res){
console.log(res)
}
})
}
为啥先下载了,手机上也无法保存??
问题1: 头像需要下载,页面需要加载
问题2:你在 wx.saveImageToPhotosAlbum 加个fail方法打印下呢