uni-app app生成带二维码海报保存图片 + 分享到微信
<div class="cnblogs_code"><pre><strong><span style="font-size: 16px; font-family: 黑体, "Heiti SC"">html代码</span></strong><br> <view style="padding-top: 100rpx;">
<!-- <view class="tips">
<image src="../static/fingerprint.png" mode=""></image>
<text></text>
</view> -->
<view class="posterImgBox">
<view id="poster">
<img class="image" :src="imageUrl" mode=""></img>
<uqrcode v-<span style="color: rgba(0, 0, 255, 1)">if</span>="!posterImgType" class="qrCodeImg" ref="uQRCode" :text="'https://www.wm319.com?page=/pages/wcatLoin/wcatLoin@userId=' + userData.id" :size="42" />
</view>
</view>
<view class="btns">
<view class="shareBtn" @click="renderScript.emitData">
<text>保存到相册</text>
</view>
<view class="shareBtn" @click="shareBtn">
<image src="../static/weixin.png" mode=""></image>
<text>分享到微信</text>
</view>
</view>
</view></pre>
</div>
<p><span style="color: rgba(255, 0, 0, 1)">注: uqrcode插件在uni-app插件市场导入项目即可直接用标签方式使用</span></p>
<div class="cnblogs_code">
<pre><strong><span style="font-size: 16px; font-family: 黑体, "Heiti SC"">JS代码(逻辑层)</span></strong><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 保存图片到本地</span>
<span style="color: rgba(0, 0, 0, 1)">downLoadImage(){
let base64 </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.imageUrl;
const bitmap </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> plus.nativeObj.Bitmap("test"<span style="color: rgba(0, 0, 0, 1)">);
bitmap.loadBase64Data(base64, </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
const url </span>= "_doc/" + <span style="color: rgba(0, 0, 255, 1)">new</span> Date().getTime() + ".png";<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> url为时间戳命名方式</span>
console.log('saveHeadImgFile'<span style="color: rgba(0, 0, 0, 1)">, url)
bitmap.save(url, {
overwrite: </span><span style="color: rgba(0, 0, 255, 1)">true</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否覆盖</span>
quality: 10<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 图片清晰度</span>
}, (i) =><span style="color: rgba(0, 0, 0, 1)"> {
uni.saveImageToPhotosAlbum({
filePath: url,
success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
uni.showToast({
title: </span>'图片保存成功'<span style="color: rgba(0, 0, 0, 1)">,
icon: </span>'none'<span style="color: rgba(0, 0, 0, 1)">
})
bitmap.clear()
}
});
}, (e) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
uni.showToast({
title: </span>'图片保存失败'<span style="color: rgba(0, 0, 0, 1)">,
icon: </span>'none'<span style="color: rgba(0, 0, 0, 1)">
})
bitmap.clear()
});
}, (e) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
uni.showToast({
title: </span>'图片保存失败'<span style="color: rgba(0, 0, 0, 1)">,
icon: </span>'none'<span style="color: rgba(0, 0, 0, 1)">
})
bitmap.clear()
});
},
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 点击分享到微信</span>
<span style="color: rgba(0, 0, 0, 1)">shareReachWeiXin(){
uni.share({
provider: </span>"weixin"<span style="color: rgba(0, 0, 0, 1)">,
scene: </span>"WXSceneSession"<span style="color: rgba(0, 0, 0, 1)">,
type: </span>0<span style="color: rgba(0, 0, 0, 1)">,
href: </span>""<span style="color: rgba(0, 0, 0, 1)">, <span style="color: rgba(0, 128, 128, 1)">// 跳转的地址</span>
title: </span>"XXXX"<span style="color: rgba(0, 0, 0, 1)">, <span style="color: rgba(0, 128, 128, 1)">// 分享的标题</span>
summary: </span>"邀请你和我一起成长"<span style="color: rgba(0, 0, 0, 1)">,
imageUrl: </span>"logo.png"<span style="color: rgba(0, 0, 0, 1)">,
success: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (res) {
console.log(</span>"success:" +<span style="color: rgba(0, 0, 0, 1)"> JSON.stringify(res));
},
fail: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (err) {
console.log(</span>"fail:" +<span style="color: rgba(0, 0, 0, 1)"> JSON.stringify(err));
}
});
},<br></span></pre>
<p> // 接收生成的图片<br> receiveRenderData(val){<br> this.imageUrl = val;<br> this.posterImgType = true;<br> this.downLoadImage();</p>
<p><em id="__mceDel"> },</em></p>
<pre><span style="color: rgba(0, 0, 0, 1)"> </span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(255, 0, 0, 1)">注:额外创建script建立renderjs</span><br><script module="renderScript" lang="renderjs"><span style="color: rgba(0, 0, 0, 1)">
import html2canvas from </span>'../../utils/html2canvas.min.js'<span style="color: rgba(0, 0, 0, 1)">;
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
data() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
}
},
mounted() {},
methods: {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送数据到逻辑层</span>
<span style="color: rgba(0, 0, 0, 1)"> emitData(e, ownerVm) {
const dom </span>= document.getElementById('poster'<span style="color: rgba(0, 0, 0, 1)">)
html2canvas(dom, {
width: dom.clientWidth, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">dom 原始宽度</span>
<span style="color: rgba(0, 0, 0, 1)"> height: dom.clientHeight,
scrollY: </span>0, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> html2canvas默认绘制视图内的页面,需要把scrollY,scrollX设置为0</span>
scrollX: 0<span style="color: rgba(0, 0, 0, 1)">,
foreignObjectRendering: </span><span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否在浏览器支持的情况下使用ForeignObject渲染</span>
useCORS: <span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否尝试使用CORS从服务器加载图像</span>
async: <span style="color: rgba(0, 0, 255, 1)">false</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 是否异步解析和呈现元素</span>
background: "#ffffff"<span style="color: rgba(0, 0, 0, 1)">,
dpi: </span>300<span style="color: rgba(0, 0, 0, 1)">
}).then((canvas) </span>=><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(e, ownerVm);</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(canvas)</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将生产的canvas转为base64图片3</span>
ownerVm.callMethod('receiveRenderData', canvas.toDataURL('image/png'<span style="color: rgba(0, 0, 0, 1)">))
});
}
}
};
</span></script></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/wtwebskill/p/15960091.html
頁:
[1]