提交 16da9f60 编写于 作者: D DCloud_LXH

feat(App): chooseImage compress

上级 f40c0f0a
import { TEMP_PATH } from '../constants'
import { warpPlusErrorCallback } from '../../../helpers/plus'
import { getFileName } from '../../../helpers/file'
import {
API_TYPE_CHOOSE_IMAGE,
API_CHOOSE_IMAGE,
......@@ -27,30 +26,6 @@ function getFileInfo(filePath: string): Promise<PlusIoMetadata> {
})
}
function compressImage(tempFilePath: string): Promise<string> {
const dst = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(
tempFilePath
)}`
return new Promise((resolve) => {
plus.nativeUI.showWaiting()
plus.zip.compressImage(
{
src: tempFilePath,
dst,
overwrite: true,
},
() => {
plus.nativeUI.closeWaiting()
resolve(dst)
},
() => {
plus.nativeUI.closeWaiting()
resolve(tempFilePath)
}
)
})
}
type File = {
path: string
size: number
......@@ -67,39 +42,15 @@ export const chooseImage = defineAsyncApi<API_TYPE_CHOOSE_IMAGE>(
function successCallback(paths: string[]) {
const tempFiles: File[] = []
const tempFilePaths: string[] = []
// plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行
paths
.reduce((promise, path) => {
return promise
.then(() => {
return getFileInfo(path)
})
.then((fileInfo) => {
const size = fileInfo.size!
// 压缩阈值 0.5 兆
const THRESHOLD = 1024 * 1024 * 0.5
// 判断是否需要压缩
if (
!crop &&
sizeType!.includes('compressed') &&
size > THRESHOLD
) {
return compressImage(path).then((dstPath) => {
path = dstPath
return getFileInfo(path)
})
}
return fileInfo
})
.then(({ size }) => {
tempFilePaths.push(path)
tempFiles.push({
path,
size: size!,
})
})
}, Promise.resolve())
.then(() => {
Promise.all(paths.map((path) => getFileInfo(path)))
.then((filesInfo) => {
filesInfo.forEach((file, index) => {
const path = paths[index]
tempFilePaths.push(path)
tempFiles.push({ path, size: file.size! })
})
resolve({
tempFilePaths,
tempFiles,
......@@ -127,6 +78,8 @@ export const chooseImage = defineAsyncApi<API_TYPE_CHOOSE_IMAGE>(
filename: TEMP_PATH + '/gallery/',
permissionAlert: true,
crop,
// @ts-expect-error
sizeType,
})
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册