博客
关于我
canvas组件绘制的内容导出生成图片保存到相册后打开异常
阅读量:458 次
发布时间:2019-03-06

本文共 2353 字,大约阅读时间需要 7 分钟。

转:

问题现象:

  canvas组件绘制的内容导出生成图片保存到相册,进入相册,打开该图片看不到绘制的内容,只有黑色的背景图。

下图中绿色背景部分是canvas组件绘制的内容。

保存到相册效果图如下:

问题分析:

  华为的快应用引擎会在每次调用getContext方法创建ctx,会返回不同的ctx对象,有的里面会有内容,有的里面会为空,获取的时候是随机获取的,可能会获取到空的ctx,导致保存后的图片没有内容,问题代码如下。

canvas绘制代码

pic() {      var test = this.$element("canvas");      var ctx = test.getContext("2d");      var img = new Image();      img.src = "http://www.huawei.com/Assets/CBG/img/logo.png";      img.onload = function () {        console.log("图片加载完成");        ctx.drawImage(img, 10, 10, 300, 300, );      }      img.onerror = function () {        console.log("图片加载shibai");      }    },

保存图片代码

save() {      var test = this.$element("canvas");      test.toTempFilePath({        fileType: "jpg",        quality: 1.0,        success: (data) => {          console.log(`Canvas toTempFilePath success ${data.uri}`)          console.log(`Canvas toTempFilePath success ${data.tempFilePath}`)          media.saveToPhotosAlbum({            uri: data.uri,            success: function (ret) {              console.log("save success: ");            },            fail: function (data, code) {              console.log("handling fail, code=" + code);            }          })        },        fail: (data) => {          console.log('Canvas toTempFilePath data =' + data);        },        complete: () => {          console.log('Canvas toTempFilePath complete.');        }      })    }

解决方法

把ctx定义为全局变量,对ctx进行非空判断,为空时,初始化保存。

优化代码如下

var ctx; //定义一个全局的    pic() {      if (!ctx) { //对空对象进行判断,ctx为空的时候,把“2d”赋给它        var test = this.$element("canvas");        var tt = test.getContext("2d");        ctx = tt;      }      var img = new Image();      img.src = "http://www.huawei.com/Assets/CBG/img/logo.png";      img.onload = function () {        console.log("图片加载完成");        ctx.drawImage(img, 10, 10, 300, 300, );      }      img.onerror = function () {        console.log("图片加载shibai");      }    }

欲了解更多详情,请参见:

快应用开发指导文档:https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-whitepaper

快应用canvas组件:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-component-canvas

画布接口:

https://developer.huawei.com/consumer/cn/doc/development/quickApp-References/quickapp-api-canvas


原文链接: https://developer.huawei.com/consumer/cn/forum/topic/0201404980467060234?fid=18

作者:AppGallery Connect

转:

转载地址:http://qozbz.baihongyu.com/

你可能感兴趣的文章
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>