這篇文章主要介紹“怎么使用uniapp組件對上傳的圖片進行壓縮至1兆以內”,在日常操作中,相信很多人在怎么使用uniapp組件對上傳的圖片進行壓縮至1兆以內問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用uniapp組件對上傳的圖片進行壓縮至1兆以內”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
original :正常
compressed:壓縮
這是組件源碼里顯示傳參的參考:
sizeType: { type: Array, default () { return ['original', 'compressed'] } },
所以在調用uni-file-picker組件時,就可以進行設置,如下:
<uni-file-picker v-model="mentouValue" return-type="object" fileMediatype="image" mode="grid" :sourceType="sourceType" :sizeType="sizeType" :auto-upload="false" @select="mentouSelect" @delete="mentouDelete"/>
:sizeType="sizeType" 表示的是壓縮圖片的設置
data中設置sizeType的值:
export default { data() { return{ mentouValue:{}, //圖片value值 sizeType:['compressed'], //設置圖片壓縮 sourceType:['camera'], //這里設置的是只能使用照相機,不能從相冊里傳照片 } }, }
通過以上設置,可以實現對圖片進行壓縮 一般是對半壓縮的,比如5兆壓縮成2.5兆左右這樣的。
如何進行檢驗:本地是檢驗不出來的,需要拿手機進行真機調試,才可以看出來文件壓縮后的大小
如果對圖片大小沒有太大限制 ,直接這樣壓縮就可以了,但是有的項目會限制對圖片的大小必須小于1兆,這時候,光有這個設置,就滿足不了需求了,這時候我們可以再采取一點措施
1、先創建一個方法imageCompress,一般單獨放在公共函數里
// 圖片壓縮遞歸,小于1M跳出 export function imageCompress(file){ return new Promise((resolve, reject)=>{ let { size,path } = file let type = path.split(".")[1] //大于1M進行壓縮, if(size< (1024*1024)){ resolve(file) return false } uni.compressImage({ src: path, quality: 80, success: res => { let newPath = res.tempFilePath+type let newName = res.tempFilePath.split("/")[res.tempFilePath.split("/").length-1]+type uni.getFileInfo({ filePath:res.tempFilePath, success:async (info)=>{ let newFile = {...file,size:info.size,path:newPath,name:newName,tempFilePath:res.tempFilePath} resolve(await imageCompress(newFile)) } }) } }) }) }
2、調用uni-file-picker組件的頁面中,調用該方法,然后再上傳圖片
import { imageCompress } from "@/utils/index.js" import { baseUrl } from "@/utils/request" export default { data() { return{ mentouValue:{}, //圖片value值 sizeType:['compressed'], //設置圖片壓縮 sourceType:['camera'], //這里設置的是只能使用照相機,不能從相冊里傳照片 file:{}, type:'', workId:'' } }, onLoad(option) { this.workId = option.workId }, methods:{ //選擇照片 mentouSelect(e){ this.timeSeting() if(e.tempFilePaths&&e.tempFiles){ this.file = e.tempFiles[0].file this.type = 'mentou' this.toUpload() } }, // 刪除照片 mentouDelete(e){ this.mentouValue = {} }, // 上傳照片 async toUpload(){ // 壓縮圖片 this.file = await imageCompress(this.file) // 要傳的參數 let params = { file:this.file, type: this.type, workId:this.workId } // 上傳圖片到相依的接口 uni.uploadFile({ url: baseUrl+'/app/uploadImage', //這里為拼接的接口地址 filePath: this.file.tempFilePath?this.file.tempFilePath:this.file.path, fileType: "image", formData:{...params}, name: 'file', header: { 'content-type': 'multipart/form-data', "Authorization": uni.getStorageSync('Authorization'), "refToken": uni.getStorageSync('refToken') }, success: uploadFileRes => { let imageName = JSON.parse(uploadFileRes.data).data // 這里可以對返回的參數進行處理了 uni.showToast({ title: '上傳成功', icon: "success" }); }, fail(err) { uni.showToast({ title: '上傳失敗', icon: "error" }); } }) }, } }
到此,關于“怎么使用uniapp組件對上傳的圖片進行壓縮至1兆以內”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。