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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。