From 0b5fbf59534f18a1f80510b8e411227246c28dde Mon Sep 17 00:00:00 2001 From: BTMuli Date: Wed, 14 Jun 2023 22:57:53 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=A4=84=E7=90=86=20html2canvas=20?= =?UTF-8?q?=E8=B7=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/saveImg.ts | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/utils/saveImg.ts b/src/utils/saveImg.ts index 99469db2..1fa293b1 100644 --- a/src/utils/saveImg.ts +++ b/src/utils/saveImg.ts @@ -6,7 +6,7 @@ */ // tauri -import { dialog, fs } from "@tauri-apps/api"; +import { dialog, fs, http } from "@tauri-apps/api"; /** * @description 保存图片-canvas @@ -15,16 +15,33 @@ import { dialog, fs } from "@tauri-apps/api"; * @param {string} filename - 文件名 * @returns {Promise} 无返回值 */ -export async function saveCanvasImg(canvas: HTMLCanvasElement, filename: string): Promise { +export async function saveCanvasImg (canvas: HTMLCanvasElement, filename: string): Promise { const buffer = new Uint8Array(atob(canvas.toDataURL("image/png").split(",")[1]).split("").map((item) => item.charCodeAt(0))); await dialog.save({ defaultPath: filename, - filters: [{ name: "图片", extensions: ["png"] }] + filters: [{ name: "图片", extensions: ["png"] }], }).then(async (res) => { if (res === null) return; await fs.writeBinaryFile({ path: res, - contents: buffer + contents: buffer, }); }); -} \ No newline at end of file +} + +/** + * @description 将图片保存到本地 + * @since Alpha v0.2.0 + * @param {string} url - 图片链接 + * @returns {Promise} 图片元素 + */ +export async function saveImgLocal (url: string): Promise { + return await http.fetch(url, { + method: "GET", + responseType: http.ResponseType.Binary, + }).then(async (res) => { + const buffer = new Uint8Array(res.data); + const blob = new Blob([buffer], { type: "image/png" }); + return URL.createObjectURL(blob); + }); +}