7***@qq.com
7***@qq.com
  • 发布:2021-05-13 18:03
  • 更新:2021-05-13 18:03
  • 阅读:544

canvasToTempFilePath导出图片是白图

分类:uni-app

uni.showLoading({
title: '裁剪中...',
})

            if(this.iszoom)  
            {  
                    this.pixelRatio=1;  
                    console.log("this is fixed")  
            }  
            else{  
            if(this.proportion!=0)  
                this.proportion=this.pixelRatio;  
            }  

            const _this = this  
            const ctx = uni.createCanvasContext('myCanvas', _this);  

            const pixelRatio = _this.pixelRatio  
            const imgage = _this.src  
            const imgW = _this.imageWidth * _this.scale;  
            const imgH = _this.imageHeight * _this.scale  
            const rotate = _this.rotate  
            let dx = _this.cropOffsertX - _this.x - (_this.imageWidth - imgW) / 2;  
            let dy = _this.cropOffsertY - _this.y - (_this.imageHeight - imgH) / 2;  

            ctx.setFillStyle('white')  
            ctx.fillRect(0, 0, imgW, imgH)  
            ctx.save()  
            ctx.rotate((rotate * 90 * Math.PI) / 180);  
            switch (rotate) {  
                case 1:  
                    dx += (imgH-imgW) / 2  
                    dy -= (imgH-imgW) / 2  
                    ctx.drawImage(imgage, -dy, dx, imgW, -imgH);  
                    break;  
                case 2:  
                    ctx.drawImage(imgage, dx, dy, -imgW, -imgH);  
                    break;  
                case 3:  
                    dx += (imgH-imgW) / 2  
                    dy -= (imgH-imgW) / 2  
                    ctx.drawImage(imgage, dy, -dx, -imgW, imgH);  
                    break;  
                default:  
                 ctx.drawImage(imgage, -dx, -dy, imgW, imgH);  
                       //ctx.drawImage(imgage, 2, 2, 375,375);  
                    break;  
            }  

            ctx.restore()  
            // #ifdef MP-ALIPAY  
            ctx.draw(true, () => {  
                ctx.toTempFilePath({  
                destWidth: _this.cropW * pixelRatio,  
                    destHeight: _this.cropH * pixelRatio,  
                    success: (res) => {  

                        uni.hideLoading()  
                     // event.detail.tempFilePath =filePath  
                            event.detail.tempFilePath =res.apFilePath  

                        _this.show = false  
                        _this.$emit('confirm', event)  
                    },  
                    fail: (e) => {  
                        uni.hideLoading()  
                        uni.showModal({  
                            title: '提示',  
                            content: '裁剪失败'  
                       })  
                    }  
                }, _this);  
            })  
            // #endif  
            // #ifndef MP-ALIPAY  
            ctx.draw(false, () => {  
                uni.canvasToTempFilePath({  
                    // width:_this.cropW,  
                    // height:_this.cropH,  
                    destWidth: _this.cropW * pixelRatio,  
                    destHeight: _this.cropH * pixelRatio,  
                    canvasId: 'myCanvas',  
                    success: (res) => {  
                        uni.hideLoading()  
                        event.detail.tempFilePath = res.tempFilePath  
                        _this.show = false  
                        _this.$emit('confirm', event)  
                    },  
                    fail: (e) => {  
                        uni.hideLoading()  
                        uni.showModal({  
                            title: '提示',  
                            content: '裁剪失败'  
                        })  
                    },  
                    complete(res) {  

                    }  
                });  

            })  
            // #endif  

目前测试出来小米手机都导出白图,其他安卓可以正常导出图片

2021-05-13 18:03 负责人:无 分享
已邀请:

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