diff --git a/src/components/ComponentToImg.js b/src/components/ComponentToImg.js
index 62c49df..28a5789 100644
--- a/src/components/ComponentToImg.js
+++ b/src/components/ComponentToImg.js
@@ -12,68 +12,106 @@ const ComponentToImg = (props) => {
const { unsplashImage } = useContext(ImgContext);
const componentRef = React.createRef();
-
-
-
async function saveImage(data) {
- var a = document.createElement("A");
- a.href = data;
- a.download = `cover.png`;
- document.body.appendChild(a);
- setLoading(false)
-
- a.click();
- document.body.removeChild(a);
- }
-
- const downloadImage = async () => {
- // exportComponentAsPNG(componentRef, 'cover')
- setLoading(true)
-
- const element = componentRef.current;
-
- // console.log(element)
- // console.log(element.offsetHeight)
-
- let data = await domtoimage.toPng(componentRef.current, {
- height: element.offsetHeight * 2,
- width: element.offsetWidth * 2,
- style: {
- transform: "scale(" + 2 + ")",
- transformOrigin: "top left",
- width: element.offsetWidth + "px",
- height: element.offsetHeight + "px",
- }
- })
-
- // console.log(data)
- await saveImage(data);
-
-
- if (unsplashImage) {
- unsplash.photos.trackDownload({ downloadLocation: unsplashImage.downloadLink, });
+ try {
+ var a = document.createElement("A");
+ a.href = data;
+ a.download = `cover.png`;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ } catch (error) {
+ console.error('保存图片失败:', error);
+ alert('下载失败,请重试');
+ } finally {
+ setLoading(false);
}
}
+ const downloadImage = async () => {
+ try {
+ setLoading(true);
+
+ const element = componentRef.current;
+
+ if (!element) {
+ throw new Error('无法找到要下载的元素');
+ }
+
+ // 等待所有图片加载完成
+ const images = element.querySelectorAll('img');
+ await Promise.all(Array.from(images).map(img => {
+ if (img.complete) return Promise.resolve();
+ return new Promise((resolve, reject) => {
+ img.onload = resolve;
+ img.onerror = () => {
+ console.warn('图片加载失败:', img.src);
+ resolve(); // 即使图片加载失败也继续
+ };
+ // 设置超时
+ setTimeout(resolve, 5000);
+ });
+ }));
+
+ // 简化配置,只使用基本选项
+ const data = await domtoimage.toPng(componentRef.current, {
+ height: element.offsetHeight * 2,
+ width: element.offsetWidth * 2,
+ style: {
+ transform: "scale(" + 2 + ")",
+ transformOrigin: "top left",
+ width: element.offsetWidth + "px",
+ height: element.offsetHeight + "px",
+ }
+ });
+
+ await saveImage(data);
+
+ // 跟踪 Unsplash 下载
+ if (unsplashImage && unsplashImage.downloadLink) {
+ try {
+ await unsplash.photos.trackDownload({
+ downloadLocation: unsplashImage.downloadLink
+ });
+ } catch (trackError) {
+ console.warn('跟踪下载失败:', trackError);
+ // 不影响主要功能,只是记录警告
+ }
+ }
+
+ } catch (error) {
+ console.error('下载图片时出错:', error);
+ setLoading(false);
+
+ // 提供更友好的错误信息
+ if (error.message.includes('CORS') || error.message.includes('cross-origin')) {
+ alert('图片下载失败:跨域访问被阻止。请尝试刷新页面后重新选择图片。');
+ } else if (error.message.includes('网络')) {
+ alert('图片下载失败:网络连接问题。请检查网络连接后重试。');
+ } else {
+ alert('图片下载失败,请重试。如果问题持续存在,请尝试选择其他图片。');
+ }
+ }
+ }
return (