zoukankan      html  css  js  c++  java
  • UNI 项目压缩图片 转BASE64上传

    本方法适用于H5和APP,其它端未测试。

    首先去http://tool.hhyang.cn/doc4/custom-picture/dataURLtoBlob.html

    下载 并引用custom-picture.js;

    然后引用

    chooseImg() {
                    // #ifdef APP-PLUS
                    plus.gallery.pick(res => {
                    
                            this.$Ctpic.dataURLtoBlob({
                                path: res,
                                // isNet: false,
                                format: 'jpg',
                                // GetBase64: true,
                                compress: 0.5,
                            }).then(res => {
                                // plus.nativeUI.toast(res)
                                let path = res.target //绝对路径
                                if (plus.os.name == "iOS") {
                                    path = plus.io.convertLocalFileSystemURL(res.target) //IOS下要转绝对路径
                                }
                                const fileReader = new plus.io.FileReader()
                                fileReader.readAsDataURL(path)
                                fileReader.onloadend = (res) => { //读取文件成功完成的回调函数
                                    // console.log(res.target.result) //输出base64内容
                                   let base64= res.target.result;
                                    
                                }
                                fileReader.onerror = err => {
                                    console.log(JSON.stringify(err))
                                }
                            });
                    }, err => {
                        plus.nativeUI.toast(err)
                    }, {
                        maximum: 1
                    })
                    // #endif
                    // #ifdef H5
                    uni.chooseImage({
                        count: 1,
                        complete: (res) => {
                            this.showCanvas = true;
                            this.src = res.tempFilePaths[0];
                            this.$refs.helangCompress.compress({
                                src: res.tempFilePaths[0],
                                maxSize: 1920,
                                fileType: 'jpg',
                                quality: 0.7
                            }).then((res) => {
                                // 压缩成功回调
                                // console.log(res)
                                let base64 = res;
                                
                            }).catch((err) => {
                                // 压缩失败回调
                                uni.hideLoading()
                            })
                        
    
                        }
                    })
                    // #endif
                },
  • 相关阅读:
    Unlocker(强力删除文件工具) 1.9.2 汉化绿色版
    js 用blob来显示存储资源,并清除其他资源
    js 创建音频声音
    兼容 线性渐变
    @font-face 兼容写法
    中国行政区域划分 爬虫工具
    前端中的spring实现
    css命名规范
    sass 备忘命令
    charles 破解命令
  • 原文地址:https://www.cnblogs.com/huzhuhua/p/13899094.html
Copyright © 2011-2022 走看看