提交 9262d99d 编写于 作者: Q qiang

fix(App): saveImage use writer

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