提交 e1619e8a 编写于 作者: Q qiang

fix(App): saveImage use writer

上级 c1eb13e7
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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册