- 效果
- 按钮
<view class="flex-item"> <button class="custom-button" @click="handleStyledButtonClick()">二维码</button> </view>
- 函数
handleStyledButtonClick() { var that = this // 接口调用 roomdata.getRoomQRCode(function() { //获取返回值base64 that.base64 = that.detailmodel.roomQRCode // 将base64字符串转换为ArrayBuffer格式 const arrayBuffer = uni.base64ToArrayBuffer(that.base64); // 将ArrayBuffer转换为Blob对象 const blob = new Blob([arrayBuffer], { type: 'image/png' }); // 创建Blob URL const imgUrl = URL.createObjectURL(blob); // 弹框显示图片 uni.previewImage({ urls: [imgUrl], }) }) }, // 将 Base64 字符串转换为 Blob 对象 base64ToBlob(base64) { const parts = base64.split(';base64,'); const contentType = parts[0].split(':')[1]; const raw = window.atob(parts[1]); const rawLength = raw.length; const uInt8Array = new Uint8Array(rawLength); for (let i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], { type: contentType }); },
- 样式
.flex-item { margin-right: 10px; } .custom-button { width: 90px; height: 20px; line-height: 20px; background-color: #2979FF; border: none; cursor: pointer; }
前端将base64转图片(二维码生成)
最新推荐文章于 2024-03-19 10:51:05 发布