【 错误总结 】API ‘getMenuButtonBoundingClientRect/‘ is not yet implemented

报错信息

API 'getMenuButtonBoundingClientRect/' is not yet implemented
在这里插入图片描述
这是api不支持 ,当前平台不支持这个api,注意添加条件编译

解决办法

H5 || APP-PLUS || MP-ALIPAY 不支持胶囊按钮样式的API

// 根据微信小程序的右上侧的胶囊样式 设置导航栏内容的高度
// #ifndef H5 || APP-PLUS || MP-ALIPAY
	const menuInfo = uni.getMenuButtonBoundingClientRect()
	// 导航栏内容的高度 = (胶囊底部的高度 - 状态栏的高度) + (胶囊顶部的高度 - 状态栏的高度)
	this.navBarHeight = (menuInfo.bottom - info.statusBarHeight) + (menuInfo.top - info.statusBarHeight)
	// 导航栏内容的宽度 = ()
	this.navBarWidth = menuInfo.left
	// console.log(this.navBarHeight,this.navBarWidth)
// #endif
你遇到的错误: > **`API 'compressImage' is not yet implemented`** 是 **uni-app 在某些平台(尤其是 H5 或部分小程序)上不支持 `uni.compressImage` API** 导致的。 --- ## 🔍 错误原因详解 ### ❌ 问题代码: ```js uni.compressImage({ src: imagePath, quality: 60, success: (res) => { ... }, fail: (err) => { console.error(err); } // <-- 这里报错:API not yet implemented }) ``` 这个 API 的支持情况如下: | 平台 | 是否支持 `uni.compressImage` | |------|----------------------------| | ✅ 微信小程序 | ✔️ 支持 | | ✅ App(iOS/Android) | ✔️ 支持(HBuilderX 2.3.0+) | | ⚠️ H5 | ❌ 不支持(抛出 `not yet implemented`) | | ⚠️ 阿里/百度/字节等其他小程序 | ❌ 大多不支持或部分支持 | 👉 所以你在 **H5 调试时**调用了该方法,就会出现这个提示。 --- ## ✅ 解决方案:兼容性处理 + 手动实现 H5 图片压缩 我们需要做两件事: 1. **判断平台,仅在支持时调用 `uni.compressImage`** 2. **在 H5 上使用 Canvas 手动压缩图片** --- ## ✅ 最终修复后的 `compressImage` 方法(完整可运行) ```js // ✅ 替换你原来的 compressImage 方法 async compressImage(imagePath) { return new Promise((resolve, reject) => { // 1. 获取图片信息 uni.getImageInfo({ src: imagePath, success: (imageInfo) => { const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); const { width, height } = imageInfo; const quality = this.compressionQuality / 100 || 0.6; // 默认 60% // 设置 canvas 尺寸 canvas.width = width; canvas.height = height; // 绘制图片到 canvas(实现缩放和压缩) const img = new Image(); img.src = imagePath; img.onload = () => { context.drawImage(img, 0, 0, width, height); // 导出为 base64 或 blob(压缩后) canvas.toBlob( (blob) => { if (blob) { // 创建临时 URL 返回 const tempFilePath = URL.createObjectURL(blob); console.log('H5 图片压缩成功', tempFilePath); resolve(tempFilePath); } else { console.warn('Canvas toBlob 失败,返回原图'); resolve(imagePath); } }, 'image/jpeg', quality ); }; img.onerror = () => { console.error('图片加载失败'); resolve(imagePath); }; }, fail: (err) => { console.error('获取图片信息失败', err); resolve(imagePath); } }); }); } ``` --- ## 🧩 使用说明 & 平台判断优化(推荐写法) 你可以进一步封装成跨平台函数: ```js async compressImage(imagePath) { return new Promise((resolve, reject) => { // ✅ 优先使用原生 API(App / 微信小程序) if (['app-plus', 'mp-weixin'].includes(process.env.UNI_PLATFORM)) { uni.compressImage({ src: imagePath, quality: this.compressionQuality, success: (res) => { resolve(res.tempFilePath); }, fail: (err) => { console.log('原生 compressImage 失败,降级到 canvas 压缩', err); this.compressImageWithCanvas(imagePath, resolve); } }); return; } // 🌐 H5 和其他平台使用 Canvas 压缩 this.compressImageWithCanvas(imagePath, resolve); }); } // 单独抽出 Canvas 压缩逻辑 compressImageWithCanvas(imagePath, resolve) { uni.getImageInfo({ src: imagePath, success: (info) => { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); canvas.width = info.width; canvas.height = info.height; const img = new Image(); img.src = info.path; img.onload = () => { ctx.drawImage(img, 0, 0); canvas.toBlob( (blob) => { if (blob) { const tempFilePath = URL.createObjectURL(blob); resolve(tempFilePath); } else { resolve(imagePath); // 降级返回原图 } }, 'image/jpeg', this.compressionQuality / 100 ); }; img.onerror = () => resolve(imagePath); }, fail: () => resolve(imagePath) }); } ``` --- ## 💡 注意事项 | 要点 | 说明 | |------|------| | 📷 `uni.compressImage` 只适用于本地临时文件路径(如拍照、选图得到的 `tempFilePaths`) | 不支持网络图片 | | 🖼️ Canvas 压缩适用于 H5,但结果是 `ObjectURL`,不能直接上传? | 可转为 Blob 再上传 | | 🔐 H5 中 `toBlob` 受跨域限制 | 确保图片同源或服务器允许 CORS | | 📦 压缩后文件大小可控吗? | 通过 `quality` 控制(建议 50~80) | --- ## ✅ 如何测试是否生效? 在 H5 浏览器打开控制台,查看: - 是否还有 `not yet implemented` 报错? - `console.log('H5 图片压缩成功')` 是否打印? - `previewPhoto` 是否仍能预览? 如果都能正常运行 → 说明已成功兼容! --- ## ✅ 补充:上传时处理 Blob(H5 场景) 如果你在 H5 上压缩后得到的是 `ObjectURL`,而 `uni.uploadFile` 不接受,可以这样处理: ```js // 把 ObjectURL 转成 File/Blob function urlToFile(url, filename = 'upload.jpg') { return fetch(url) .then(res => res.blob()) .then(blob => new File([blob], filename, { type: 'image/jpeg' })); } ``` 然后用 FormData 上传(需后端配合)。 ---
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值