uniapp 分享微信及分享到朋友圈(方法2中),分享海报(原生 app),及uni-app 中使用 html2canvas 生成图片

1.方法一 微信转发及分享到朋友圈

参考链接
在这里插入图片描述

2.方法二 微信转发及分享到朋友圈

shareUrl(this.id).then(response => { // 获取二维码跳转地址
                    switch (e.item.name) {
                        case 'wxchum': // 重点是这里
                            uni.share({
                                provider: 'weixin',
                                scene: 'WXSceneSession',
                                type: 0,
                                href: response.share_url,
                                title: this.goods.name,
                                summary: this.goods.description,
                                imageUrl: this.goods.pictures[0],
                                fail(err) {
                                    uni.showToast({
                                        title: err.errMsg,
                                        icon: 'none'
                                    })
                                }
                            })

                            break;
                        case 'wxcircle':
                            uni.share({
                                provider: 'weixin',
                                scene: 'WXSceneTimeline',
                                type: 0,
                                href: response.share_url,
                                summary: this.goods.description,
                                imageUrl: this.goods.pictures[0],
                                fail(err) {
                                    uni.showToast({
                                        title: err.errMsg,
                                        icon: 'none'
                                    })
                                }
                            })
                            break;
                        case 'download':
                            this.canSave = true
                            uni.showLoading({
                                title: '保存中'
                            })
                            this.down()
                            break;
                    }
                }).catch(err => {
                    return
                })

3.生成海报-方法一(app原生截屏)

 down() {
                setTimeout(() => {
                    let pages = getCurrentPages();
                    let page = pages[pages.length - 1];
                    let ws = page.$getAppWebview();
                    let bitmap = new plus.nativeObj.Bitmap('drawScreen');
                    // 将webview内容绘制到Bitmap对象中  
                    ws.draw(bitmap, () => {
                        // 保存图片到本地  
                        bitmap.save("_doc/drawScreen.jpg", {
                            overwrite: true
                        }, res => {
                            uni.saveImageToPhotosAlbum({ //保存图片到系统相册。
                                filePath: res.target, //图片文件路径
                                success: () => {
                                    this.canSave = false;
                                    uni.showToast({
                                        title: '保存图片成功,赶紧去转发给好友吧~',
                                        icon: 'none'
                                    })
                                },
                                fail: function(e) {
                                    uni.showModal({
                                        title: '温馨提示',
                                        content: '保存图片失败,重新再试试~',
                                        showCancel: false,
                                        confirmColor: "#34CE98",
                                        confirmText: '知道了',
                                        success: () => {
                                            this.canSave = false;
                                        }
                                    })
                                    uni.hideLoading()
                                }
                            });

                            bitmap.clear(); // 清除Bitmap对象  
                        }, error => {
                            uni.showModal({
                                title: '温馨提示',
                                content: '保存图片失败,重新再试试~',
                                showCancel: false,
                                confirmColor: "#34CE98",
                                confirmText: '知道了',
                                success: () => {
                                    this.canSave = false;
                                }
                            })
                            uni.hideLoading()
                            bitmap.clear(); // 清除Bitmap对象  
                        });
                        // bitmap.clear(); // 清除Bitmap对象  
                    }, error => {
                        uni.showModal({
                            title: '温馨提示',
                            content: '保存图片失败,重新再试试~',
                            showCancel: false,
                            confirmColor: "#34CE98",
                            confirmText: '知道了',
                            success: () => {
                                this.canSave = false;
                            }
                        })
                        uni.hideLoading()
                    }, {
                        check: true, // 设置为检测白屏  
                    });
                }, 2000)
            },

4.uni-app 中使用 html2canvas 生成图片

在使用uni-app 开发的过程中有没有遇到 无法使用html2canvas生成图片的问题呢?那么通过这篇文章,我们来记录下如何在uniapp生成画布图片的。
首先需要在你的uni-app项目中安装 html2canvas 插件

npm install html2canvas -D

或者

yarn add html2canvas -D

在需要生成图片的页面中引入 html2canvas 插件
不过此时需要在 页面中新建一个 script 节点,将 lang 属性设置为renderjs,代码如下:

<script>
export default {
  data() {
    return {};
  }
};
</script>

// 在这里新增一个 script 节点 用于在页面中使用dom
<script lang="renderjs" module="canvasImage">
import html2canvas from 'html2canvas'
export default {
}
</script>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值