简体   繁体   English

Blob 图像不会显示在 UI 中,尽管我可以在控制台上看到它们

[英]Blob images does not show up at UI, although I can see them on console

I am trying to create multiple blob images, and show them for a video timeline UI.我正在尝试创建多个 blob 图像,并将它们显示为视频时间线 UI。 To create the images, I take screenshots of the video for some certain time frames, and create img elements, then add img.src as blob images' url.为了创建图像,我截取了某些时间帧的视频截图,并创建了 img 元素,然后将 img.src 添加为 blob 图像的 url。 The problem is blob images does not show up at UI, although I can see them on console .问题是blob 图像没有显示在 UI 中,尽管我可以在 console 上看到它们 Any ideas?有任何想法吗?

Code snippet from creation of blob images:来自创建 blob 图像的代码片段:

// draw the video frame to canvas
const ctx = canvas.getContext("2d");
ctx.drawImage(videoPlayer, 0, 0, canvas.width, canvas.height);
ctx.canvas.toBlob(
    blob => {
        resolve(blob);
    },
    "image/jpeg",
    0.75 /* quality */
);

Code Snippet from creation of images:来自创建图像的代码片段:

let img = '';
for (let i = 0; i < count / 3; i++) {
    const cover = await GetVideoCover(item.video_url, i);
    var url = URL.createObjectURL(cover);
    var image = new Image();
    image.src = url;
    img += "<img src='"+image.src+"'>";
    console.log(img)
}
return img;

The console log from console.log(img) line:来自console.log(img)行的控制台日志:

在此处输入图像描述

actual html img elements:实际 html img 元素:

在此处输入图像描述

When I manually update the source as below, it works, but I did not understand why it does not get the source by itself.当我如下手动更新源时,它可以工作,但我不明白为什么它不能自己获取源。 Any idea would be appreciated.任何想法将不胜感激。

在此处输入图像描述

Try registering an onload event listener on the image element and then handle all code that depends on that image in the event listener, like so:尝试在image元素上注册一个onload事件侦听器,然后在事件侦听器中处理依赖于该图像的所有代码,如下所示:

for (let i = 0; i < count / 3; i++) {
    const cover = await GetVideoCover(item.video_url, i);
    var url = URL.createObjectURL(cover);
    var image = new Image();
    image.src = url;
    image.addEventListener('load', (event) => {
    // perform some magic here e.g append to DOM or draw the image to canvas
    }
}

Thank you for the contributions, the answers did not work but they helped me in the process.感谢您的贡献,答案没有用,但他们在此过程中帮助了我。 I solved the problem today and wanted to share it here.我今天解决了这个问题,并想在这里分享。

  1. I should have mentioned before as the created images are fed to vis.js .我之前应该提到过,因为创建的图像被馈送到vis.js
  2. When I experimented more, I saw vis.js does not append the created objects, but it changes the innerHTML of the timeline.当我进行更多实验时,我看到 vis.js 并没有 append 创建的对象,但它改变了时间轴的 innerHTML。 (vis.js might not support blob images as src of img tag) (vis.js 可能不支持 blob 图像作为 img 标签的 src)
  3. Then, I decided to give a shot to append element.然后,我决定试一试 append 元素。 To do that, I created a new div element, and appended the created images to that div.为此,我创建了一个新的 div 元素,并将创建的图像附加到该 div。
  4. I also added an additional promise level to the blob image creation process, since some files were missing.我还在 blob 映像创建过程中添加了一个额外的 promise 级别,因为缺少一些文件。

Code snippet from newly added promise level:新添加的 promise 级别的代码片段:

const blobToImage = (itemVideoURL,captureSecond) => {
    return new Promise(async (resolve,reject)=> {
        let cover = await GetVideoCover(itemVideoURL,captureSecond);
        const url = URL.createObjectURL(cover)
        let img = new Image()
        img.onload = () => {
        URL.revokeObjectURL(url)
        resolve(img)
        }
        img.src = url
        img.height = '75'
    })
}

Code snippet from new function:来自新 function 的代码片段:

let divElement  = document.createElement('div');
for (let i = 0; i < count * 3; i++) {
    let newImage = await blobToImage(item.video_url,i+1)
    divElement.appendChild(newImage)
}
return divElement;

It magically worked.它神奇地起作用了。

You should use a single image rather than multiple images.您应该使用单个图像而不是多个图像。

Just concatanate all images together:只需将所有图像连接在一起:

   let imgEle1 = document.querySelectorAll(".image")[0];
   let imgEle2 = document.querySelectorAll(".image")[1];
   let resEle = document.querySelector(".result");
   var context = resEle.getContext("2d");
   let BtnEle = document.querySelector(".Btn");
   BtnEle.addEventListener("click", () => {
   resEle.width = imgEle1.width;
      resEle.height = imgEle1.height;
      context.globalAlpha = 1.0;
      context.drawImage(imgEle1, 0, 0);
      context.globalAlpha = 0.5;
      context.drawImage(imgEle2, 0, 0);
   });

暂无
暂无

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

相关问题 如何在控制台上查看我上传的图片名称? - How I can see my uploading images' names on console? <span>虽然我可以在firebug中看到它,但</span>加载页面的Javascript响应没有显示出来 - Javascript response from a loaded page is not showing up in <span> although i can see it in firebug 带有 useFieldArray 的 React-Hook Form:TextField 值未显示在控制台中。 我该如何解决? - React-Hook Form with useFieldArray: TextField value does not show up in the console. How can I fix it? 如何缩放HTML5画布,但不缩放标记图像,又如何将它们显示在正确的位置? - How can I scale an HTML5 Canvas but not scale marker images and still have them show up in the right place? 如何打印我在控制台中看到的 Firebase 数组元素,以在网页上显示它们? - How can I print the Firebase array elements that I see in the console, to display them on the webpage? 如何实现 cloudinary 动态显示图像作为背景? 我看不到正确获取它们 - How can I implement cloudinary to display images as backgrounds dynamically? I can't see to fetch them correctly 出现图像时如何为图像添加过渡效果? - How can I add transition effect to images when they show up? React &amp; Axios:无法在 UI 中呈现数组值,但我可以控制台记录它们 - React & Axios: Unable to render array values in UI but I can console.log them Firestore还没有返回任何记录,我在控制台中看到了它们 - Firestore returning no records yet, I see them in console 尽管console.log显示正确的值,但文本字段对Reducer没有反应 - Textfield does not react to Reducer, although console.log show correct value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM