简体   繁体   English

如何获取和显示 blob 图像

[英]How to fetch and display blob images

I am trying to save images to indexeddb and then fetch and display them in a react app.我正在尝试将图像保存到 indexeddb,然后在反应应用程序中获取并显示它们。 My approach is to convert images to a blob and save the blob url to indexeddb and then when it's time to display, fetch the blob url and set the image url as.src.我的方法是将图像转换为 blob 并将 blob url 保存到 indexeddb,然后在显示时,获取 blob url 并将图像设置为 Z572D4E421E5E06B9BC12ZErcE275。

How I created the blob我如何创建 blob

 fetch(imgurl, { mode: 'no-cors', method: "get", headers: { "Content-Type": "application/json" } }).then(response => response.blob()).then(async images => { var blob_url = URL.createObjectURL(images) var blobA = blob_url.replace("blob:","") var blobB = blobA.replace('"','')

this is bloburl blob:http://localhost:3000/d7a55f39-b496-476b-8c3c-857874bd107c这是 bloburl blob:http://localhost:3000/d7a55f39-b496-476b-8c3c-857874bd107c

I then remove blob: and save this url in indexeddb to be fetched when needed http://localhost:3000/d7a55f39-b496-476b-8c3c-857874bd107c然后我删除 blob: 并将此 url 保存在 indexeddb 中,以便在需要时获取 http://localhost:3000/d7a55f39-b496-476b-8c3c-857874bd107

this is where I need to use the blob image这是我需要使用 blob 图像的地方

 import React from "react"; import { Row, Col, Container, Image } from 'react-bootstrap'; function Item({ item }) { const imgurl = item.productimg fetch(imgurl).then(res => res.blob()) // Gets the response and returns it as a blob.then(async blob => { const test = await blobToImage(blob) console.log("image test",test) let objectURL = URL.createObjectURL(blob); let myImage = document.getElementById('myImg') myImage.src = objectURL; }); return ( <div className="item" id={item.id}> <Row> <Col> <Image id="myImg" width="50" height="58" rounded /> </Col> <Col xs={6}> <div className="item-info"> <p>{item.name}</p> </div> </Col> <Col>3 of 3</Col> </Row> <div className="item-actions"> <button className="btn-remove">X</button> </div> </div> ); } export default Item;

Item contains all the productdata including the saved blob url.项目包含所有产品数据,包括保存的 Blob url。 The problem is the images are not showing and I could not figure out why.问题是图像没有显示,我不知道为什么。

what could i be doing wrong and how can I display the images我可能做错了什么以及如何显示图像

The URL lifetime is tied to the document in the window on which it was created. URL 生命周期与创建它的 window 中的文档相关联。 . . ObjectURL is not meant to be saved somewhere. ObjectURL 并不意味着要保存在某个地方。 You can convert your image to base64 and store as data-uri instead.您可以将图像转换为base64 并存储为 data-uri

Try this code for your image.为您的图像尝试此代码。

Example App:示例应用程序:

I've updated a React example on StackBlitz在 StackBlitz 上更新了一个 React 示例

React <Item /> component that will work: React <Item />组件将起作用:


function Item({ item }) {
  const imgurl = item.productimg;
  const imageRef = React.useRef();
  React.useEffect(() => {
    fetch(imgurl)
      .then(res => res.blob()) // Gets the response and returns it as a blob
      .then(blob => {
        let objectURL = URL.createObjectURL(blob);
        console.log(imageRef.current);
        imageRef.current.src = objectURL;
      });
  }, []);

  return (
    <div className="item" id={item.id}>
      <Row>
        <Col>
          <Image ref={imageRef} id="myImg" width="50" height="58" rounded />
        </Col>
        <Col xs={6}>
          <div className="item-info">
            <p>{item.name}</p>
          </div>
        </Col>
        <Col>3 of 3</Col>
      </Row>

      <div className="item-actions">
        <button className="btn-remove">X</button>
      </div>
    </div>
  );
}

Javascript: Javascript:

 const imgurl = "https://cdn62.picsart.com/182788826000202.jpg?type=webp&to=crop&r=256" fetch(imgurl).then(response => response.blob()).then(myBlob => { var urlCreator = window.URL || window.webkitURL; var imageUrl = urlCreator.createObjectURL(myBlob); const myImgElem = document.getElementById('my-img'); myImgElem.src = imageUrl })
 <img id="my-img" />

Good Luck...祝你好运...

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

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