无法使用 Navigator.share 共享带有文本的图像

问题描述 投票:0回答:1

我想分享带有标题的图像。这里我有使用

html-to-image
插件动态生成图像的代码。

但是,当我分享图像(WhatsApp、Facebook)时,文本将被丢弃。

另一方面,如果我将两个文件作为图像传递并尝试共享它们,文本将包含在这两个图像中。

getScoreImage() {
    this.dynamicImageEle = document.getElementById('quiz-share-image-wrap');

    const v_this = this;

        htmlToImage.toPng(this.dynamicImageEle)
        .then(function (dataUrl) {
        var img = new Image();
        img.src = dataUrl;
        v_this.dynamicImageBase64 = img.src;
        const file = v_this.dataURLtoFile(dataUrl, "ipl-quiz.png");

        setTimeout(()=> {
            v_this.shareFile(file);
        },100)
    })
    .catch(function (error) {
        console.error('oops, something went wrong!', error);
    });
}

dataURLtoFile(dataurl: any, filename:any) {
    var arr = dataurl.split(","),
        mimeType = arr[0].match(/:(.*?);/)[1],
        decodedData = atob(arr[1]),
        lengthOfDecodedData = decodedData.length,
        u8array = new Uint8Array(lengthOfDecodedData);
    while (lengthOfDecodedData--) {
        u8array[lengthOfDecodedData] = decodedData.charCodeAt(lengthOfDecodedData);
    }
    return new File([u8array], filename, { type: mimeType });
    
};

async shareFile(file:any) {
    if (navigator.canShare && navigator.canShare({ files: [file] })) {
        await navigator.share({
            "title": this.shareTitle,
            "text": this.shareText,
            "files": [file, file],
        })
        .then(() => console.log("Share was successful."))
        .catch((error) => console.log("Sharing failed", error));
    } else {
        console.log(`Your system doesn't support sharing files.`);
    }
};

Current output with image and no text

If I pass two images then output is proper but 2 image is not expected

预期输出是带有文本的图像

javascript angular typescript web navigator
1个回答
0
投票

此 API 在使用时存在一些不一致的情况。我目前也面临着同样的问题; 现在,我可以通过构建动态图像(文本重叠)然后共享它来克服它。

或者我会通过链接(图像预览)提供图像并与其共享文本。

抱歉,我无法在评论部分添加此答案,因为我没有足够的声誉。

© www.soinside.com 2019 - 2024. All rights reserved.