weixin_43356655 2023-02-27 15:54 采纳率: 83.3%
浏览 18
已结题

小程序wx.canvasToTempFilePath压缩图片出现Uncaught (in promise) undefined错误

小程序使用wx.canvasToTempFilePath传入jpeg的图片就会报错:Uncaught (in promise) undefined
jpg和png的图片测试了没问题。这个压缩的太n了,整了一天都没整出来.


            fileList.map((item, idx) => {
                compressList[idx] = new Promise((resolve, reject) => {
                    wx.createSelectorQuery().select('#imageCanvas').fields({
                        node: true,
                        size: true
                    }).exec((res) => {
                        const canvas = res[0].node;
                        const ctx = canvas.getContext('2d')
                        const image = canvas.createImage()
                        image.src = item.url
                        image.onload = () => {
                            let ratio = 2
                            let canvasWidth = image.width
                            let canvasHeight = image.height
                            while (canvasWidth > 500 && canvasHeight > 500) {
                                canvasWidth = Math.trunc(res.width / ratio)
                                canvasHeight = Math.trunc(res.height / ratio)
                                ratio++
                            }
                            canvas.width = canvasWidth
                            canvas.height= canvasHeight
                            ctx.fillRect(0, 0, canvasWidth, canvasHeight)
                            ctx.drawImage(image, 0, 0, canvasWidth, canvasHeight)
                            
                            wx.canvasToTempFilePath({
                                canvas,
                                quality: 0.5,
                                fileType: 'jpg',
                                width: canvasWidth / 2, //canvas的宽高(生成图片的范围)
                                heght: canvasHeight / 2,
                                destWidth: (canvasWidth / 2) * wx.getWindowInfo().pixelRatio,
                                destHeight: (canvasHeight / 2) * wx.getWindowInfo().pixelRatio,
                                success: (res) => {
                                    console.log(res);
                                    resolve(res.tempFilePath)
                                },
                                fail: (res) => {
                                    reject()
                                }
                            })
                        }
                    })
                }).then(result => result)
            })

  • 写回答

2条回答 默认 最新

  • 简效 2023-02-27 16:27
    关注

    在微信开发者工具,右边详情里,把基础库版本调低试一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月8日
  • 已采纳回答 2月28日
  • 创建了问题 2月27日

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗