// 保存截图
            downloadFile() {
                let $this = this;
                uni.showLoading({ //加载框
                    title: '保存中...',
                    mask: true
                })
                var pages = getCurrentPages(); //获取当前页面信息
                var page = pages[pages.length - 1];
                var bitmap = null;
                // $this.$nextTick(()=> {
                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: '截图成功',
                                    duration: 1500
                                });
                            },
                            complete() {
                                uni.hideLoading();
                            }
                        });
                    }, function(e) {
                        console.log('保存图片失败:' + JSON.stringify(e));
                    });
                }, function(e) {
                    console.log('保存图片失败:' + JSON.stringify(e));
                });
                // })
            },

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐