小程序
小游戏
企业微信
微信支付
扫描小程序码分享
fileType:'jpg',选择这个格式,发现保存的临时文件的后缀的确变成了jpg,但是奇怪的是文件格式还是png格式的,这个是用微信开发者工具测试的结果,请问官方如何解决
2 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
由于开发者工具有 BUG,只好手工粘贴代码在这里
注:经测试,貌似些 BUG 只会在开发者工具出现,手机端好像不会出现
js 文件
// js Page({ data: { canvas_info: { width: 2382, height: 2406, cols: 6, rows: 3, uWidth: 397, uHeight: 802 }, img: { url: "https://chiz.img.jwwvip.com/images/6252dbea397e68b740d1e21f.jpg?x-oss-process=image/resize,m_mfit,l_800,s_395", height: 800, width: 395, } }, onLoad() { const that = this; wx.createSelectorQuery().select('#scheme') .fields({ node: true }) .exec(res => { const canvas = res[0].node; const canvas_info = that.data.canvas_info; const img = that.data.img; that.draw_image(canvas, canvas_info, img.width, img.height); }); }, draw_image(canvas, canvas_info, sWidth, sHeight) { const that = this; const ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); canvas.width = canvas_info.width; canvas.height = canvas_info.height; const img_opts = that.get_img_opts(canvas_info, sWidth, sHeight); let success_cnt = 0; wx.showLoading({ title: '正在生成中', mask: true, }); img_opts.forEach(opt => { const img = canvas.createImage(); img.onload = () => { ctx.drawImage( img, opt.sx, opt.sy, opt.sWidth, opt.sHeight, opt.dx, opt.dy, opt.dWidth, opt.dHeight, ); success_cnt += 1; if (success_cnt === img_opts.length) { wx.canvasToTempFilePath({ canvas: canvas, quality: 0.8, fileType: "jpg", success: res => { console.log(res.tempFilePath); that.setData({ path: res.tempFilePath }); wx.hideLoading(); } }); } }; img.src = opt.url; }); }, get_img_opts: function (canvas_info, sWidth, sHeight) { const that = this; const result = []; for (let i = 0; i < canvas_info.rows; i++) { for (let j = 0; j < canvas_info.cols; j++) { result.push({ url: that.data.img.url, sx: 0, sy: 0, sWidth: sWidth, sHeight: sHeight, dx: canvas_info.uWidth * j, dy: canvas_info.uHeight * i, dWidth: sWidth, dHeight: sHeight, }); } } return result; }, preview () { const that = this; wx.previewImage({ urls: [that.data.path], }); }, save() { const that = this; wx.saveImageToPhotosAlbum({ filePath: that.data.path, }); } });
wxml 文件
<view class="img_box" style="height:432rpx;"> <canvas bindtap="preview" id="scheme" type="2d" style="width:400rpx; height:400rpx"></canvas> </view> <button type="primary" bindtap="save">保存</button>
wxss 文件
.img_box { width: 100%; display: flex; justify-content: center; align-items: center; border-bottom: 1px solid rgba(128, 128, 128, 0.2); }
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。
由于开发者工具有 BUG,只好手工粘贴代码在这里
注:经测试,貌似些 BUG 只会在开发者工具出现,手机端好像不会出现
js 文件
// js Page({ data: { canvas_info: { width: 2382, height: 2406, cols: 6, rows: 3, uWidth: 397, uHeight: 802 }, img: { url: "https://chiz.img.jwwvip.com/images/6252dbea397e68b740d1e21f.jpg?x-oss-process=image/resize,m_mfit,l_800,s_395", height: 800, width: 395, } }, onLoad() { const that = this; wx.createSelectorQuery().select('#scheme') .fields({ node: true }) .exec(res => { const canvas = res[0].node; const canvas_info = that.data.canvas_info; const img = that.data.img; that.draw_image(canvas, canvas_info, img.width, img.height); }); }, draw_image(canvas, canvas_info, sWidth, sHeight) { const that = this; const ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); canvas.width = canvas_info.width; canvas.height = canvas_info.height; const img_opts = that.get_img_opts(canvas_info, sWidth, sHeight); let success_cnt = 0; wx.showLoading({ title: '正在生成中', mask: true, }); img_opts.forEach(opt => { const img = canvas.createImage(); img.onload = () => { ctx.drawImage( img, opt.sx, opt.sy, opt.sWidth, opt.sHeight, opt.dx, opt.dy, opt.dWidth, opt.dHeight, ); success_cnt += 1; if (success_cnt === img_opts.length) { wx.canvasToTempFilePath({ canvas: canvas, quality: 0.8, fileType: "jpg", success: res => { console.log(res.tempFilePath); that.setData({ path: res.tempFilePath }); wx.hideLoading(); } }); } }; img.src = opt.url; }); }, get_img_opts: function (canvas_info, sWidth, sHeight) { const that = this; const result = []; for (let i = 0; i < canvas_info.rows; i++) { for (let j = 0; j < canvas_info.cols; j++) { result.push({ url: that.data.img.url, sx: 0, sy: 0, sWidth: sWidth, sHeight: sHeight, dx: canvas_info.uWidth * j, dy: canvas_info.uHeight * i, dWidth: sWidth, dHeight: sHeight, }); } } return result; }, preview () { const that = this; wx.previewImage({ urls: [that.data.path], }); }, save() { const that = this; wx.saveImageToPhotosAlbum({ filePath: that.data.path, }); } });
wxml 文件
<view class="img_box" style="height:432rpx;"> <canvas bindtap="preview" id="scheme" type="2d" style="width:400rpx; height:400rpx"></canvas> </view> <button type="primary" bindtap="save">保存</button>
wxss 文件
.img_box { width: 100%; display: flex; justify-content: center; align-items: center; border-bottom: 1px solid rgba(128, 128, 128, 0.2); }