目錄
簡(jiǎn)介
使用方式
NPM install 安裝
直接下載
API
pathToBase64
base64ToPath
提示:多個(gè)任務(wù)可以串行或者并行執(zhí)行
一款非常好用的插件,它就是 image-tools?
簡(jiǎn)介
圖像轉(zhuǎn)換工具,可用于如下環(huán)境:uni-app、微信小程序、5+APP、瀏覽器(需允許跨域)。
使用方式
NPM install 安裝
npm i image-tools --save
頁(yè)面引入js 即可使用
import { pathToBase64, base64ToPath } from 'image-tools'
直接下載
// 以下路徑需根據(jù)項(xiàng)目實(shí)際情況填寫(xiě)
import { pathToBase64, base64ToPath } from '../../js/image-tools/index.js'
API
pathToBase64
從圖像路徑轉(zhuǎn)換為base64,uni-app、微信小程序和5+APP使用的路徑不支持網(wǎng)絡(luò)路徑,如果是網(wǎng)絡(luò)路徑需要先使用下載API下載下來(lái)。
頁(yè)面調(diào)用方式
pathToBase64(path)
? .then(base64 => {
? ? console.log(base64)
? })
? .catch(error => {
? ? console.error(error)
? })
base64ToPath
將圖像base64保存為文件,返回文件路徑。
base64ToPath(base64)
? .then(path => {
? ? console.log(path)
? })
? .catch(error => {
? ? console.error(error)
? })
提示:多個(gè)任務(wù)可以串行或者并行執(zhí)行
可以利用promise來(lái)?串行?和?并行?的執(zhí)行多個(gè)任務(wù)
// 并行
Promise.all(paths.map(path => pathToBase64(path)))
? .then(res => {
? ? console.log(res)
? ? // [base64, base64...]
? })
? .catch(error => {
? ? console.error(error)
? })
// 串行
paths.reduce((promise, path) => promise.then(res => pathToBase64(path).then(base64 => (res.push(base64), res))), Promise.resolve([]))
? .then(res => {
? ? console.log(res)
? ? // [base64, base64...]
? })
? .catch(error => {
? ? console.error(error)
? })
人生苦短,拒絕內(nèi)卷。我是跟著飛哥學(xué)編程,一個(gè)一心向陽(yáng),向陽(yáng)而生,努力向上生長(zhǎng)的年輕人。加油兄弟們……
致力于實(shí)際工作場(chǎng)景中的避坑經(jīng)驗(yàn)分享,無(wú)套路,純干貨。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-482949.html
愛(ài)飛哥,沒(méi)毛病,飛哥帶你上高速,沖沖沖……文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-482949.html
到了這里,關(guān)于uni-app 中圖片轉(zhuǎn) base64 以及 base64 轉(zhuǎn)圖片方式,超簡(jiǎn)單,超好用的圖片轉(zhuǎn)換工具,你值得擁有它。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!