diff --git a/src/platforms/app-plus/helpers/save-image.js b/src/platforms/app-plus/helpers/save-image.js index 057eeeb9e045262d7f98c4bdf9ad9f7d3c138f60..e672a2b5d9a48d521cf64d2c1ce8a0975b1615e1 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) }