提交 b6d25351 编写于 作者: D DCloud_LXH

feat(App): chooseImage compress

上级 3a1c557e
...@@ -7,8 +7,7 @@ import { ...@@ -7,8 +7,7 @@ import {
} from '../../bridge' } from '../../bridge'
import { import {
warpPlusErrorCallback, warpPlusErrorCallback
getFileName
} from '../util' } from '../util'
import { import {
...@@ -20,7 +19,7 @@ import { ...@@ -20,7 +19,7 @@ import {
* @param {string} filePath 文件路径 * @param {string} filePath 文件路径
* @returns {Promise} 文件信息Promise * @returns {Promise} 文件信息Promise
*/ */
function getFileInfo (filePath) { function getFileInfo(filePath) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
plus.io.resolveLocalFileSystemURL(filePath, function (entry) { plus.io.resolveLocalFileSystemURL(filePath, function (entry) {
entry.getMetadata(resolve, reject, false) entry.getMetadata(resolve, reject, false)
...@@ -28,25 +27,7 @@ function getFileInfo (filePath) { ...@@ -28,25 +27,7 @@ function getFileInfo (filePath) {
}) })
} }
function compressImage (tempFilePath) { export function chooseImage({
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, count,
sizeType, sizeType,
sourceType, sourceType,
...@@ -54,59 +35,46 @@ export function chooseImage ({ ...@@ -54,59 +35,46 @@ export function chooseImage ({
} = {}, callbackId) { } = {}, callbackId) {
const errorCallback = warpPlusErrorCallback(callbackId, 'chooseImage', 'cancel') const errorCallback = warpPlusErrorCallback(callbackId, 'chooseImage', 'cancel')
function successCallback (paths) { function successCallback(paths) {
const tempFiles = [] const tempFiles = []
const tempFilePaths = [] const tempFilePaths = []
// plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行 // plus.zip.compressImage 压缩文件并发调用在iOS端容易出现问题(图像错误、闪退),改为队列执行
paths.reduce((promise, path) => { Promise.all(paths.map((path) => getFileInfo(path)))
return promise.then(() => { .then((filesInfo) => {
return getFileInfo(path) filesInfo.forEach((file, index) => {
}).then(fileInfo => { const path = paths[index]
const size = fileInfo.size tempFilePaths.push(path)
// 压缩阈值 0.5 兆 tempFiles.push({ path, size: file.size })
const THRESHOLD = 1024 * 1024 * 0.5 })
// 判断是否需要压缩
if (!crop && sizeType.includes('compressed') && size > THRESHOLD) { invoke(callbackId, {
return compressImage(path).then(dstPath => { errMsg: 'chooseImage:ok',
path = dstPath tempFilePaths,
return getFileInfo(path) tempFiles
})
}
return fileInfo
}).then(({ size }) => {
tempFilePaths.push(path)
tempFiles.push({
path,
size
}) })
}) })
}, Promise.resolve()).then(() => { .catch(errorCallback)
invoke(callbackId, {
errMsg: 'chooseImage:ok',
tempFilePaths,
tempFiles
})
}).catch(errorCallback)
} }
function openCamera () { function openCamera() {
const camera = plus.camera.getCamera() const camera = plus.camera.getCamera()
camera.captureImage(path => successCallback([path]), camera.captureImage(path => successCallback([path]),
errorCallback, { errorCallback, {
filename: TEMP_PATH + '/camera/', filename: TEMP_PATH + '/camera/',
resolution: 'high', resolution: 'high',
crop crop
}) })
} }
function openAlbum () { function openAlbum() {
plus.gallery.pick(({ files }) => successCallback(files), errorCallback, { plus.gallery.pick(({ files }) => successCallback(files), errorCallback, {
maximum: count, maximum: count,
multiple: true, multiple: true,
system: false, system: false,
filename: TEMP_PATH + '/gallery/', filename: TEMP_PATH + '/gallery/',
permissionAlert: true, permissionAlert: true,
crop crop,
sizeType
}) })
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册