From e1619e8aac85bac9b2760d473cc5ce4b8f672943 Mon Sep 17 00:00:00 2001 From: qiang Date: Tue, 15 Feb 2022 20:29:22 +0800 Subject: [PATCH] fix(App): saveImage use writer --- src/platforms/app-plus/helpers/save-image.js | 53 +++++++++++--------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/platforms/app-plus/helpers/save-image.js b/src/platforms/app-plus/helpers/save-image.js index 057eeeb9e..e672a2b5d 100644 --- a/src/platforms/app-plus/helpers/save-image.js +++ b/src/platforms/app-plus/helpers/save-image.js @@ -1,28 +1,35 @@ let index = 0 -export default function saveImage (base64, dirname, callback) { +export default function saveImage (dataURL, dirname, callback) { const id = `${Date.now()}${index++}` - const bitmap = new plus.nativeObj.Bitmap(`bitmap${id}`) - bitmap.loadBase64Data(base64, function () { - const format = (base64.match(/data:image\/(\S+?);/) || [null, 'png'])[1].replace('jpeg', 'jpg') - const tempFilePath = `${dirname}/${id}.${format}` - bitmap.save(tempFilePath, { - overwrite: true, - quality: 100, - format - }, function () { - clear() - callback(null, tempFilePath) - }, function (error) { - clear() - callback(error) - }) - }, function (error) { - clear() - callback(error) - }) + const array = dataURL.split(',') + const scheme = array[0] + const base64 = array[1] + const format = (scheme.match(/data:image\/(\S+?);/) || [null, 'png'])[1].replace('jpeg', 'jpg') + const fileName = `${id}.${format}` + const tempFilePath = `${dirname}/${fileName}` - function clear () { - bitmap.clear() - } + const i = dirname.indexOf('/') + const basePath = dirname.substring(0, i) + const dirPath = dirname.substring(i + 1) + plus.io.resolveLocalFileSystemURL(basePath, function (entry) { + entry.getDirectory(dirPath, { + create: true, + exclusive: false + }, function (entry) { + entry.getFile(fileName, { + create: true, + exclusive: false + }, function (entry) { + entry.createWriter(function (writer) { + writer.onwrite = function () { + callback(null, tempFilePath) + } + writer.onerror = callback + writer.seek(0) + writer.writeAsBinary(base64) + }, callback) + }, callback) + }, callback) + }, callback) } -- GitLab