uniapp踩坑之项目:canvas第一次保存是空白图片

在ctx.draw()回调生成图片,参考canvasToTempFilePath接口文档

javascript 复制代码
// data
imgFilePath: null,// 缓存二维码图片canvas路径

//js
// 首先在draw()里进行本地存储
......
ctx.draw(false, () = >{
	uni.canvasToTempFilePath({ // 把画布转化成临时文件
		quality: 1,
		// 图片质量
		canvasId: 'shareCanvas',
		// 画布ID
		success: (res) = >{
			// uni.showToast({
			//  icon: 'success',
			//  mask: true,
			//  title: '绘制完成',
			// });
			this.imgFilePath = res.tempFilePath;
			//console.log(this.imgFilePath, 'this.imgFilePath');
			uni.setStorageSync('filePath', this.imgFilePath) //保存临时文件路径到缓存
		},
		fail: function(res) {
			// console.log(res);
		}
	})
})

// 然后保存图片按钮进行取值
// 保存图片到本地,下面保存到手机
async savePic() {
	// console.log(this.imgFilePath, 'this.imgFilePathsavePic')
	let res = await uni.canvasToTempFilePath({ // 把画布转化成临时文件
		quality: 1,
		// 图片质量
		canvasId: 'shareCanvas',
	}) // 画布ID
	let storageImgFilePath = uni.getStorageSync('filePath')
	// console.log(storageImgFilePath,'storageImgFilePath');
	setTimeout(() = >{
		uni.saveImageToPhotosAlbum({ // 存成图片至手机
			filePath: storageImgFilePath,
			success: (res2) = >{
				uni.showToast({
					title: '图片保存成功',
					duration: 2000
				})
			},

			............
		})
	},
	1000)

上一篇文章,

vue3+elementPlus:el-drawer新增修改弹窗复用_element plus drawer onclick outside 嵌套弹窗-CSDN博客文章浏览阅读854次,点赞5次,收藏6次。vue3+elementPlus:el-drawer新增修改弹窗复用。在el-drawer的属性里设置:title属性,和重置函数。_element plus drawer onclick outside 嵌套弹窗https://blog.csdn.net/weixin_43928112/article/details/135237566

相关推荐
t***265919 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
蓝胖子的多啦A梦19 小时前
ElementUI表格错位修复技巧
前端·css·vue.js·el-table表格错位
_OP_CHEN19 小时前
前端开发实战深度解析:(一)认识前端和 HTML 与开发环境的搭建
前端·vscode·html·web开发·前端开发
Irene199119 小时前
ES6 export 语句 语法规范
javascript·es6·export
xiAo_Ju19 小时前
iOS一个Fancy UI的Tricky实现
前端·ios
H***997619 小时前
Vue深度学习实战
前端·javascript·vue.js
游戏开发爱好者820 小时前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone
猴猴不是猴20 小时前
js实现卷轴,中间可滑动方块,左右两侧对比
javascript·css·css3
toooooop820 小时前
Vuex 中 state、mutations 和 actions 的原理和写法
前端·javascript·uni-app
y***866920 小时前
前端CSS-in-JS方案
前端·javascript·css