小编给大家分享一下微信小程序如何基于Taro实现分享图片的功能。 希望大家读完这篇文章后有所收获。 我们一起来讨论一下吧!
Taro支持语言小程序的开发,支持CSS预处理器,支持实时编译和更新,支持NPM等等。 这不太酷!
微信小程序中分享图片的功能经常出现在小程序的业务中,比如学习打卡分享、推广会员分享、推广产品分享等等。 因为小程序不支持直接分享图片到朋友圈,所以一般操作是:
今天胡哥给大家分享一下基于Taro框架实现微信小程序图片分享功能的实践。
1.搭建Taro项目框架并创建微信小程序
1.安装taro脚手架工具
npm install -g @tarojs/cli
2.初始化taro项目
taro init taro-img-share
3、编译工程,打开Dev模式,生成小程序--dist目录
npm run dev:weapp
4.微信开发者工具,创建一个小程序,选择项目根目录为taro-img-下的dist目录
2、小程序分享图片功能实践---签到图片分享功能
先上图,后讲
效果图
点击保存到相册
这就是重点:用来绘制图片并显示,保存图片到相册
()方法负责绘制和显示,()方法负责下载图片到相册
src ///.js
import Taro, { Component } from '@tarojs/taro' // 引入对应的组件 import { View, Text, Button, Canvas } from '@tarojs/components' import './index.scss' export default class Index extends Component { config = { navigationBarTitleText: '首页' } /** * 初始化信息 */ constructor () { this.state = { // 用户信息 userInfo: {}, // 是否展示canvas isShowCanvas: false } } /** * getUserInfo() 获取用户信息 */ getUserInfo (e) { if (!e.detail.userInfo) { Taro.showToast({ title: '获取用户信息失败,请授权', icon: 'none' }) return } this.setState({ isShowCanvas: true, userInfo: e.detail.userInfo }, () => { // 调用绘制图片方法 this.drawImage() }) } /** * drawImage() 定义绘制图片的方法 */ async drawImage () { // 创建canvas对象 let ctx = Taro.createCanvasContext('cardCanvas') // 填充背景色 let grd = ctx.createLinearGradient(0, 0, 1, 500) grd.addColorStop(0, '#1452d0') grd.addColorStop(0.5, '#FFF') ctx.setFillStyle(grd) ctx.fillRect(0, 0, 400, 500) // // 绘制圆形用户头像 let { userInfo } = this.state let res = await Taro.downloadFile({ url: userInfo.avatarUrl }) ctx.save() ctx.beginPath() // ctx.arc(160, 86, 66, 0, Math.PI * 2, false) ctx.arc(160, 88, 66, 0, Math.PI * 2) ctx.closePath() ctx.clip() ctx.stroke() ctx.translate(160, 88) ctx.drawImage(res.tempFilePath, -66, -66, 132, 132) ctx.restore() // 绘制文字 ctx.save() ctx.setFontSize(20) ctx.setFillStyle('#FFF') ctx.fillText(userInfo.nickName, 100, 200) ctx.setFontSize(16) ctx.setFillStyle('black') ctx.fillText('已在胡哥有话说公众号打卡20天', 50, 240) ctx.restore() // 绘制二维码 let qrcode = await Taro.downloadFile({ url: 'https://upload-images.jianshu.io/upload_images/3091895-f0b4b900390aec73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/258/format/webp.jpg' }) ctx.drawImage(qrcode.tempFilePath, 70, 260, 180, 180) // 将以上绘画操作进行渲染 ctx.draw() } /** * saveCard() 保存图片到本地 */ async saveCard () { // 将Canvas图片内容导出指定大小的图片 let res = await Taro.canvasToTempFilePath({ x: 0, y: 0, width: 400, height: 500, destWidth: 360, destHeight: 450, canvasId: 'cardCanvas', fileType: 'png' }) let saveRes = await Taro.saveImageToPhotosAlbum({ filePath: res.tempFilePath }) if (saveRes.errMsg === 'saveImageToPhotosAlbum:ok') { Taro.showModal({ title: '图片保存成功', content: '图片成功保存到相册了,快去发朋友圈吧~', showCancel: false, confirmText: '确认' }) } else { Taro.showModal({ title: '图片保存失败', content: '请重新尝试!', showCancel: false, confirmText: '确认' }) } } render () { let { isShowCanvas } = this.state return ( 打卡 {/* 使用Canvas绘制分享图片 */} { isShowCanvas && 保存到相册 } ) } }
src ///.sass
.js 组件中的 css 样式
.index { display: flex; align-items: center; justify-content: center; height: 100%; } .canvas-wrap { width: 100%; height: 100%; background: rgba(0, 0, 0, 0.3); position: fixed; top: 0; left: 0; display: flex; align-items: center; justify-content: center; flex-direction: column; .btn-save { margin-top: 40rpx; } }
防范措施
设置Taro支持ES6/
1.下载@/-
npm install -D @tarojs/async-await
2.app.js中引入
import '@tarojs/async-await'
开发完成后,发布小程序
1.执行打包生成最终的小程序源码
npm run build:weapp
2. 发布小程序
点击微信开发者工具的上传按钮,上传小程序,然后将小程序发布到微信小程序平台。
概括
本文重点介绍利用Taro实现生成打卡图片、保存相册、分享图片的小程序的开发原理和实践步骤。 您可以结合自己的实际业务进行参考和拓展开发。
本文不对不同手机做深入的图像适配。 有些值也设置固定值(如填充文本的起始坐标和填充文本的长度和大小),并且不按比例响应。 需要进一步交流的朋友可以关注胡友硕哥公众号,持续关注相关文章,或者直接在文章中留言进行交流。
无论使用什么样的框架微信小程序开发设置背景图片,如Taro、Wepy等来开发小程序中分享图片的功能,原理都是一样的,只是在调用方法和处理逻辑时需要做一些小调整
看完这篇文章,相信您对“微信小程序中如何实现基于Taro的图片分享功能”有了一定的了解。 如果您想了解更多,请关注易速云行业资讯频道,感谢您的阅读!