uniapp点击按钮 保存页面为图片到本地

uniapp点击按钮 保存页面为图片到本地

首先写一个按钮 创建事件

<button class="info" @click="capture()">点击保存我哦~</button>

然后把点击事件写上

capture() {
    
    
				var pages = getCurrentPages(); //获取当前页面信息
				var page = pages[pages.length - 1];
				var bitmap = null;
				var currentWebview = page.$getAppWebview();
				bitmap = new plus.nativeObj.Bitmap('amway_img');
				// 将webview内容绘制到Bitmap对象中
				currentWebview.draw(bitmap, function() {
    
    
					// console.log('截屏绘制图片成功');
					//这里我将文件名用四位随机数拼接了,不然会出现当前图片替换上一张图片只能保存一张图片的问题
					let rand = Math.floor(Math.random() * 10000)
					let saveUrl = '_doc/' + rand + 'a.jpg'
					bitmap.save(saveUrl, {
    
    }, function(i) {
    
    
						// console.log('保存图片成功:' + JSON.stringify(i));
						uni.saveImageToPhotosAlbum({
    
    
							filePath: i.target,
							success: function() {
    
    
								// bitmap.clear(); //销毁Bitmap图片
								uni.showToast({
    
    
									title: '保存图片成功',
									mask: false,
									duration: 1500
								});
							}
						});
					}, function(e) {
    
    
						console.log('保存图片失败:' + JSON.stringify(e));
					});
				}, function(e) {
    
    
					console.log('截屏绘制图片失败:' + JSON.stringify(e));
				});
				//currentWebview.append(amway_bit);
			},

这样就可以了 可以看一下 本地相册了

Guess you like

Origin blog.csdn.net/weixin_47284756/article/details/115351367