z***@gmail.com
z***@gmail.com
  • 发布:2020-09-16 19:14
  • 更新:2021-11-25 18:32
  • 阅读:2220

【报Bug】【紧急紧急紧急】在canvas中绘制图片后,无法 canvasToTempFilePath 导出图片。100%复现。

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: win10

HBuilderX类型: 正式

HBuilderX版本号: 2.8.8

手机系统: Android

手机系统版本号: Android 10

手机厂商: 小米

手机机型: 小米9Pro 5G

页面类型: vue

打包方式: 云端

项目创建方式: HBuilderX

操作步骤:
<template>  
    <view class="content">  
        <image class="logo" :src="canvasPic" style="background-color: #fcc;"></image>  
        <canvas canvas-id="posterCanvas" style="width:320px;height:240px;background: #ccf;"></canvas>  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                canvasPic :  ''  
            }  
        },  
        onLoad() {  

        },  
        methods: {  
      async downloadPic(url) {  
        return new Promise((resolve, reject) => {  
          uni.downloadFile({  
            url: url,  
            success: res => {  
              resolve(res.tempFilePath);  
            },  
            fail: () => {  
              reject('图片下载失败');  
            }  
          });  
        });  
      },  
        },  

    async mounted() {  
      let ctx = uni.createCanvasContext('posterCanvas');  

      ctx.setFillStyle('#0f0');  
      ctx.fillText("哈哈哈1", 30, 30);  

      let pic;  

      pic = '/static/logo.png';  
      ctx.drawImage(pic, 0, 0, 100, 100);  

      pic = await this.downloadPic('https://img-cdn-qiniu.dcloud.net.cn/uniapp/doc/uniapp4@2x.png');  
      ctx.drawImage(pic, 100, 140, 200, 60);  

      ctx.draw(true, ()=>{  
        uni.canvasToTempFilePath({  
          canvasId: 'posterCanvas',  
          x: 0,  
          y: 0,  
          width: 320,  
          height: 240,  
          destWidth: 320,  
          destHeight: 240,  
          success: res => {  
            this.canvasPic = res.tempFilePath;  
          },  
          complete: (rs) => {  
            console.log('canvasToTempFilePath', rs)  
          }  
        });  
      });  
    }  
    }  
</script>  

预期结果:

canvas正常生成最终图片

实际结果:

js报错 ,图片生成失败

bug描述:

后续补充:锅应该来自MIUI升级,因为我将之前打包的App安装后,依旧有这个问题(之前测试的时候是不存在的)。。。 但是但是,依旧请官方看看小米到底给你们挖了什么坑,解决此问题。。。 小米 webview版本: 85.0.4183.101

在canvas中绘制图片后,无法 canvasToTempFilePath 导出图片,直接报错。

TypeError: Cannot read property 'data' of undefined at view.umd.min.js:1

另外,之前 2.8.11 版本也有此问题 ,降级到2.8.8之后依旧发生 。

其他手机不确定,但是小米9Rro 5G, MIUI12 开发板最新版本, 100%毕现。下方代码可以直接重现。

<template>  
    <view class="content">  
        <image class="logo" :src="canvasPic" style="background-color: #fcc;"></image>  
        <canvas canvas-id="posterCanvas" style="width:320px;height:240px;background: #ccf;"></canvas>  
    </view>  
</template>  

<script>  
    export default {  
        data() {  
            return {  
                canvasPic :  ''  
            }  
        },  
        onLoad() {  

        },  
        methods: {  
      async downloadPic(url) {  
        return new Promise((resolve, reject) => {  
          uni.downloadFile({  
            url: url,  
            success: res => {  
              resolve(res.tempFilePath);  
            },  
            fail: () => {  
              reject('图片下载失败');  
            }  
          });  
        });  
      },  
        },  

    async mounted() {  
      let ctx = uni.createCanvasContext('posterCanvas');  

      ctx.setFillStyle('#0f0');  
      ctx.fillText("哈哈哈1", 30, 30);  

      let pic;  

      pic = '/static/logo.png';  
      ctx.drawImage(pic, 0, 0, 100, 100);  

      pic = await this.downloadPic('https://img-cdn-qiniu.dcloud.net.cn/uniapp/doc/uniapp4@2x.png');  
      ctx.drawImage(pic, 100, 140, 200, 60);  

      ctx.draw(true, ()=>{  
        uni.canvasToTempFilePath({  
          canvasId: 'posterCanvas',  
          x: 0,  
          y: 0,  
          width: 320,  
          height: 240,  
          destWidth: 320,  
          destHeight: 240,  
          success: res => {  
            this.canvasPic = res.tempFilePath;  
          },  
          complete: (rs) => {  
            console.log('canvasToTempFilePath', rs)  
          }  
        });  
      });  
    }  
    }  
</script>  
2020-09-16 19:14 负责人:无 分享
已邀请:

最佳回复

DCloud_UNI_GSQ

DCloud_UNI_GSQ

HBuilderX alpha 2.9.0+ 已修复

  • chenli

    cli版本v2.0.0-alpha-28920200911001 这个修复是不是已经解决此问题了?github commit

    2020-09-17 11:50

  • DCloud_UNI_GSQ

    回复 chenli: 使用 2.0.0-alpha-28920200917001

    2020-09-17 12:06

LeoHuang

LeoHuang

我运气比你好,我2.8.8可以导出来,2.8.11完全不能用,看更新日志说2.8.11优化了加载速度,我看来,这是负向优化。
早上提了问题也没人回复

  • z***@gmail.com (作者)

    泪崩.....

    2020-09-16 19:29

  • z***@gmail.com (作者)

    我发现 2.8.11 版本有问题,然后回退到 2.8.8 版本,发现问题依旧存在。。。

    2020-09-16 19:31

  • DCloud_UNI_GSQ

    真的吗?

    2020-09-17 11:45

  • DCloud_UNI_GSQ

    这是你提的问题:https://ask.dcloud.net.cn/question/107131 我从昨天就开始验证和跟进

    2020-09-17 11:47

  • kim猴

    我也不可以,华为mate20不可以。也是报TypeError: Cannot read property 'data' of undefined at view.umd.min.js:1

    2020-09-22 18:17

弄月

弄月

华为手机也是一样,这应该是uni-app的问题

全盘崩坏

全盘崩坏 - 此颜差矣!

不光华为,我目前手头的安卓手机全部沦陷

小糊涂酒仙

小糊涂酒仙

安卓全部不能使用该方法

小糊涂酒仙

小糊涂酒仙

使用 canvasToTempFilePath 方法生成图片,触发就报错[object DOMException] at view.umd.min.js:1
HB 的版本是3.2.9.20210927 的

this.canvas_text = this.shangbiao_text;
const ctx = uni.createCanvasContext('imgCanvas');
ctx.font = 'bold 18rpx serif'
ctx.setTextAlign('center')
ctx.setFillStyle("#000000")
ctx.fillText(this.canvas_text, this.windowWidth 0.55 / 2, 60, this.windowWidth 0.55)
ctx.draw(true,
() => {
setTimeout(function() {
uni.canvasToTempFilePath({
canvasId: 'imgCanvas',
// fileType: 'png',
x: 0,
y: 0,
width: 6,
height: 100,
destWidth: 6,
destHeight: 100,
success: function(res) {
console.log(res,"pppp")
// console.log(res.tempFilePath)
// this.imgPath = res.tempFilePath
// return
// console.log(this.imgPath)
// 在这里保存图片
},
fail: function(error) {
console.log(error,"////////")
}
})
}, 100)
})

设置代码

小糊涂酒仙

小糊涂酒仙

https://ask.dcloud.net.cn/question/132043

1***@qq.com

1***@qq.com - 啊是等级划分

后面加个this试下

该问题目前已经被锁定, 无法添加新回复