微信小程序海报

记录一下微信小程序使用 Skyline 渲染引擎生成页面海报的流程。

1、单独给页面开启 Skyline 渲染引擎,在 app.json 增加如下配置:

javascript 复制代码
{
  "lazyCodeLoading": "requiredComponents",
  "rendererOptions": {
    "skyline": {
      "disableABTest": true,
      "sdkVersionBegin": "3.0.1",
      "sdkVersionEnd": "15.255.255"
    }
  }
}

2、在页面的配置文件中增加如下配置:

javascript 复制代码
{
  "usingComponents": {},
  "renderer": "skyline",
  "navigationStyle": "custom",
  "componentFramework": "glass-easel",
}

3、在 wxml 文件中使用 snapshot 组件包裹要生成海报的 view

html 复制代码
<snapshot id="poster" mode="view">
  <view class="poster">
    ...
  </view>
</snapshot>

4、生成海报图片

javascript 复制代码
this.createSelectorQuery().select("#poster")
    .node()
    .exec(res => {
      const node = res[0].node
      node.takeSnapshot({
        type: 'file',
        format: 'png',
        success: (res) => {
          this.saveImageToLocal(res.tempFilePath);
        },
        fail(res) {
          console.log('保存失败', res);
        }
      })
})

5、海报图片保存到本地

javascript 复制代码
  saveImageToLocal(tempFilePath) {
    wx.saveImageToPhotosAlbum({
      filePath: tempFilePath,
      success: (res) => {
        wx.showToast({
          title: '保存成功',
          icon: 'success'
        });
      },
      fail: (err) => {
        console.log('err', err);
        if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          // 用户拒绝了权限
          wx.showModal({
            title: '提示',
            content: '您已拒绝保存图片,请手动开启相册权限',
            showCancel: false,
            confirmText: '去设置',
            success: (res) => {
              if (res.confirm) {
                wx.openSetting();
              }
            }
          });
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          });
        }
      }
    });
  }

注意:

1、如果页面使用了第三方组件库会出现样式错乱问题;

2、PC端的小程序开发者工具目前只支持 webview 渲染,不支持 Skyline 渲染;

3、Skyline 渲染默认使用Flex布局,flex-direction 默认是 column;

相关推荐
2501_915921436 小时前
穿越HTTPS迷雾:Wireshark中的TLS抓包秘诀与文件合并方法
网络协议·ios·小程序·https·uni-app·wireshark·iphone
PinTrust SSL证书6 小时前
Sectigo(Comodo)企业型OV通配符SSL
网络·网络协议·网络安全·小程序·https·ssl
头发还在的女程序员7 小时前
家政系统源码,上门家政源码,支持小程序、APP、H5和公众号,可直接搭建使用
小程序·生活·家政
小徐_23337 小时前
uni-app 组件库 Wot UI 2.0 发布了,我们带来了这些改变!
前端·微信小程序·uni-app
黄华SJ520it7 小时前
天美仕商城平台开发代码
小程序·软件需求·系统开发
Greg_Zhong8 小时前
微信小程序中实现自定义颜色选择器(简陋版对比精致版)
微信小程序·自定义颜色选择器面板
小离a_a9 小时前
uniapp小程序添加路由全局限制,增加路由白名单,登录后接口跳转参数正常传递
小程序·uni-app
Giggle12189 小时前
家政维修保洁预约上门服务小程序软件开发解析
大数据·小程序·产品运营·个人开发·内容运营
宋拾壹9 小时前
php网站小程序接入抖音团购核销
android·小程序·php
杰建云1679 小时前
2026年第三方平台制作微信小程序多少钱?
微信小程序·小程序·小程序制作