中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何将Pixmap保存到本地文件?
微信扫码分享
static async savePixelMap(pixelMap: image.PixelMap, path: string, name: string, format: string = 'image/png'): Promise<string> { try { if (!fs.accessSync(path)) { fs.mkdirSync(path, true); //如果文件夹不存在就创建 } let filePath = path + "/" + name; let file = fs.openSync(path, fs.OpenMode.READ_WRITE); let packOpts: image.PackingOption = { format: format, quality: 100 } const imagePacker: image.ImagePacker = image.createImagePacker(); await imagePacker.packToFile(pixelMap, file.fd, packOpts).finally(() => { imagePacker.release(); //释放 }); fs.closeSync(file.fd);//关闭文件 return filePath; } catch (err) { let error = err as BusinessError; console.log(`ImageUtil-savePixelMap-异常 ~ code: ${error.code} -·- message: ${error.message}`); return ''; } }