uniapp关于app与h5图片压缩

复制代码
getPhoto() {
                let that = this;
                uni.chooseImage({
                    count: 1, 
                    sizeType: ['compressed'], 
                    sourceType: ['album', 'camera'], 
                    success: function(res) {
                        // #ifdef APP-PLUS
                        that.compressImg(res)
                        // #endif
                        // #ifdef H5
                        that.getImageInfo(res.tempFilePaths[0])
                        // #endif
                    },
                    complete: function(res) {
                        uni.hideLoading()
                    }
                });
            },
            
getImageInfo(src) { let _this
= this uni.getImageInfo({ src, success(res) { let canvasWidth = res.width //图片原始长宽 let canvasHeight = res.height let ratio = canvasWidth / canvasHeight; if (canvasWidth > 500) { canvasWidth = 500; canvasHeight = Math.floor(canvasWidth / ratio); } let img = new Image() img.src = res.path let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d') canvas.width = canvasWidth canvas.height = canvasHeight ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight) canvas.toBlob(function(fileSrc) { let imgSrc = window.URL.createObjectURL(fileSrc) _this.upimg(imgSrc) }) } }) }, compressImg(res){ uni.compressImage({ src: res.tempFilePaths[0], quality: 80, success: info => { this.upimg(info.tempFilePath) } }) }, upimg(info){ uni.showLoading({ title: '上传中...' }) var that = this uni.uploadFile({ url: '', //接口地址 filePath: info, name: 'file', success: (uploadFileRes) => { uni.hideLoading() console.log(uploadFileRes); }, fail: function(res) { uni.hideLoading() uni.showModal({ title: '提示', content: '上传失败,请重新上传', showCancel: false, }) } }); },
复制代码

 

posted @   西瓜南瓜哈密瓜  阅读(531)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示