基于uni-app的小程序端的上传图片并进行图片压缩

<image :src="img" @tap.stop="clickImg"></image>

// 第四步:在canvas绘制图片需要,不能隐藏!!!
<canvas canvas-id="firstCanvas" id="firstCanvas" :style="'width:' + canvasWidth +'px;height:' + canvasHeight + 'px;position:absolute;left:-10000px;top:-10000px'"></canvas>
clickImg() {
    // 1. 选择图片
    uni.chooseImage({
        count: 1, //一次最多选择数量
        sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album'], // 从相册选择
        success: function (res) {
            if(res.tempFiles[0].size >= 10485760) {
                // 提示:文件大于10M,请重新选择!
                return;
            }
            // 2. 获取图片宽高
            uni.getImageInfo({
                src: res.tempFilePaths[0],
                success (ress) {
                    // 3. 获取设备像素比,不获取最后图片展示有问题
                    wx.getSystemInfo({
                        success: function(info) {
                            // 4. 在canvas绘制图片
                            const ctx = uni.createCanvasContext("firstCanvas", that);
                            that.canvasWidth = ress.width/info.pixelRatio;
                            that.canvasHeight = ress.height/info.pixelRatio;
                            ctx.drawImage(ress.path, 0, 0, that.canvasWidth, that.canvasHeight);
                            ctx.draw(false, setTimeout(function() {
                                // 5. 压缩图片,不支持type=2d!!!
                                uni.canvasToTempFilePath({
                                    x: 0,
                                    y: 0,
                                    width: that.canvasWidth,
                                    height: that.canvasHeight,
                                    destWidth: that.canvasWidth,
                                    destHeight: that.canvasHeight,
                                    canvasId: "firstCanvas",
                                    quality: .5,
                                    success: function(data) {
                                        // 6. 将压缩的图片转变为二进制流
                                        wx.getFileSystemManager().readFile({
                                            filePath: data.tempFilePath,
                                            encoding: "base64",
                                            success: datas => {
                                                // 将二进制流文件复制给image,进行展示
                                                that.img = "data:image/jpeg;base64," + datas.data;
                                            },
                                            fail: (e) => {
                                                // 提示: 图片压缩失败
                                            },
                                        })
                                    },
                                    fail: (e) => {
                                        // 提示:图片信息获取失败
                                    }
                                }, that);
                            }, 1000));
                        }
                    })
                },
                fail: (e) => {
                    // 提示:图片信息获取失败
                }
            })
        },
        fail: (e) => {
            if(e.errMsg !== "chooseImage:fail cancel") {
                // 提示:上传失败
            }
        },
    });
},
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值