From 253a0156fc5906631494fef4a81696cdfc3f026f Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 31 Aug 2021 13:41:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(App):=20chooseImage=20=E6=8B=8D=E7=85=A7?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/api/media/choose-image.js | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/platforms/app-plus/service/api/media/choose-image.js b/src/platforms/app-plus/service/api/media/choose-image.js index 11d857be0..ef8b6f311 100644 --- a/src/platforms/app-plus/service/api/media/choose-image.js +++ b/src/platforms/app-plus/service/api/media/choose-image.js @@ -7,7 +7,8 @@ import { } from '../../bridge' import { - warpPlusErrorCallback + warpPlusErrorCallback, + getFileName } from '../util' import { @@ -27,6 +28,24 @@ function getFileInfo (filePath) { }) } +function compressImage (tempFilePath) { + const dstPath = `${TEMP_PATH}/compressed/${Date.now()}_${getFileName(tempFilePath)}` + return new Promise((resolve) => { + plus.nativeUI.showWaiting() + plus.zip.compressImage({ + src: tempFilePath, + dst: dstPath, + overwrite: true + }, () => { + plus.nativeUI.closeWaiting() + resolve(dstPath) + }, () => { + plus.nativeUI.closeWaiting() + resolve(tempFilePath) + }) + }) +} + export function chooseImage ({ count, sizeType, @@ -58,7 +77,21 @@ export function chooseImage ({ function openCamera () { const camera = plus.camera.getCamera() - camera.captureImage(path => successCallback([path]), + camera.captureImage(path => { + // fix By Lxh 暂时添加拍照压缩逻辑,等客户端增加逻辑后修改 + // 判断是否需要压缩 + if (sizeType && sizeType.includes('compressed')) { + return getFileInfo(path).then(({size}) => { + // 压缩阈值 0.5 兆 + const THRESHOLD = 1024 * 1024 * 0.5 + return size && size > THRESHOLD + ? compressImage(path).then(dstPath => successCallback([dstPath])) + :successCallback([path]) + }).catch(errorCallback) + } + + return successCallback([path]) + }, errorCallback, { filename: TEMP_PATH + '/camera/', resolution: 'high', -- GitLab