提交 e1619e8a 编写于 作者: Q qiang

fix(App): saveImage use writer

上级 c1eb13e7
let index = 0 let index = 0
export default function saveImage (base64, dirname, callback) { export default function saveImage (dataURL, dirname, callback) {
const id = `${Date.now()}${index++}` const id = `${Date.now()}${index++}`
const bitmap = new plus.nativeObj.Bitmap(`bitmap${id}`) const array = dataURL.split(',')
bitmap.loadBase64Data(base64, function () { const scheme = array[0]
const format = (base64.match(/data:image\/(\S+?);/) || [null, 'png'])[1].replace('jpeg', 'jpg') const base64 = array[1]
const tempFilePath = `${dirname}/${id}.${format}` const format = (scheme.match(/data:image\/(\S+?);/) || [null, 'png'])[1].replace('jpeg', 'jpg')
bitmap.save(tempFilePath, { const fileName = `${id}.${format}`
overwrite: true, const tempFilePath = `${dirname}/${fileName}`
quality: 100,
format
}, function () {
clear()
callback(null, tempFilePath)
}, function (error) {
clear()
callback(error)
})
}, function (error) {
clear()
callback(error)
})
function clear () { const i = dirname.indexOf('/')
bitmap.clear() 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)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册