简体   繁体   English

来自已给定图像 Blob URL 的图像 Blob URL

[英]Image Blob URL from already given Image Blob URL

I have a doubt with image blob,is it possible to create an Image blob URL with a given image blob URL?我对图像 blob 有疑问,是否可以使用给定的图像 blob URL 创建图像 blob URL? I created a new image blob URL and tried to use it on img tag but it did not work:我创建了一个新的图像 blob URL 并尝试在 img 标签上使用它,但它不起作用:

    useEffect(() => {
        if (socket.current) {
                socket.current.on("msg-recieve", (msg) => {
                const RecieveBlob = new Blob([msg], { type: 'image/jpeg' });
                const RecieveImageURL = URL.createObjectURL(RecieveBlob);
                console.log("msg recieved is ", msg)
                setArrivalMessage({ fromSelf: false, message: msg })
            })
        }
        setNoImage(false)
    }, [])

In this code, msg is an image blob URL coming from the server and I want to display that image in the page.在此代码中,msg 是来自服务器的图像 blob URL,我想在页面中显示该图像。 Therefore, I created new Image blob from it.因此,我从中创建了新的 Image blob。 However, when I use the new Image blob it is not working.但是,当我使用新的 Image blob 时,它不起作用。

If anybody knows why please help me, Thanks.如果有人知道为什么请帮助我,谢谢。

If the msg is String you shouldn't wrap that in brackets because it makes it an [String] or Array of strings so try this:如果 msg 是String你不应该把它放在方括号中因为它使它成为[String]或字符串数组所以试试这个:

instead of: const RecieveBlob = new Blob([msg], { type: 'image/jpeg' }) ;而不是: const RecieveBlob = new Blob([msg], { type: 'image/jpeg' }) ;

write: const RecieveBlob = new Blob(msg, { type: 'image/jpeg' }) ;写入: const RecieveBlob = new Blob(msg, { type: 'image/jpeg' }) ;

PS But I think you can use the blob URL and set it as an image source in the first place, it would work. PS但我认为您可以首先使用 blob URL 并将其设置为图像源,它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM