• <legend id='GvO5V'><style id='GvO5V'><dir id='GvO5V'><q id='GvO5V'></q></dir></style></legend>

    <i id='GvO5V'><tr id='GvO5V'><dt id='GvO5V'><q id='GvO5V'><span id='GvO5V'><b id='GvO5V'><form id='GvO5V'><ins id='GvO5V'></ins><ul id='GvO5V'></ul><sub id='GvO5V'></sub></form><legend id='GvO5V'></legend><bdo id='GvO5V'><pre id='GvO5V'><center id='GvO5V'></center></pre></bdo></b><th id='GvO5V'></th></span></q></dt></tr></i><div id='GvO5V'><tfoot id='GvO5V'></tfoot><dl id='GvO5V'><fieldset id='GvO5V'></fieldset></dl></div>

  • <small id='GvO5V'></small><noframes id='GvO5V'>

    1. <tfoot id='GvO5V'></tfoot>
        <bdo id='GvO5V'></bdo><ul id='GvO5V'></ul>

        如何将图像存储和检索到localStorage?

        How to store and retrieve image to localStorage?(如何将图像存储和检索到localStorage?)

            <small id='SV9uO'></small><noframes id='SV9uO'>

                <legend id='SV9uO'><style id='SV9uO'><dir id='SV9uO'><q id='SV9uO'></q></dir></style></legend>
                  <tbody id='SV9uO'></tbody>
                • <bdo id='SV9uO'></bdo><ul id='SV9uO'></ul>

                • <i id='SV9uO'><tr id='SV9uO'><dt id='SV9uO'><q id='SV9uO'><span id='SV9uO'><b id='SV9uO'><form id='SV9uO'><ins id='SV9uO'></ins><ul id='SV9uO'></ul><sub id='SV9uO'></sub></form><legend id='SV9uO'></legend><bdo id='SV9uO'><pre id='SV9uO'><center id='SV9uO'></center></pre></bdo></b><th id='SV9uO'></th></span></q></dt></tr></i><div id='SV9uO'><tfoot id='SV9uO'></tfoot><dl id='SV9uO'><fieldset id='SV9uO'></fieldset></dl></div>
                  <tfoot id='SV9uO'></tfoot>

                • 本文介绍了如何将图像存储和检索到localStorage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以为我有这个,但没有.目标:拍一张照片(保险卡),将其保存在本地,稍后再检索.

                  Thought I had this, but no. The goal: snap a photo (insurance card), save it locally, and retrieve it later.

                  // Get a reference to the image element
                  var elephant = document.getElementById("SnapIt_mobileimage_5");
                  
                  var imgCanvas = document.createElement("canvas"),
                  imgContext = imgCanvas.getContext("2d");
                  
                  // Make sure canvas is as big as the picture
                  imgCanvas.width = elephant.width;
                  imgCanvas.height = elephant.height;
                  
                  // Draw image into canvas element
                  
                  imgContext.drawImage(elephant, 0, 0, elephant.width, elephant.height );
                   console.log( 'Did that' );
                  // Get canvas contents as a data URL
                  var imgAsDataURL = imgCanvas.toDataURL("data:image/jpg;base64,");
                  
                  // Save image into localStorage
                  try {
                  localStorage.setItem("elephant", imgAsDataURL);
                  }
                  catch (e) {
                  console.log("Storage failed: " + e);
                  }; 
                  
                  //Did it work?
                  var pic = localStorage.getItem("elephant");
                  
                  console.log( elephant );
                  console.log( pic );
                  

                  每一步都成功,最终输出为:

                  Each step succeeds, the final output is:

                  <img id="SnapIt_mobileimage_5" class=" SnapIt_mobileimage_5" name="mobileimage_5" dsid="mobileimage_5" src="files/views/assets/image/IMG_0590.JPG">
                   data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA
                  

                  在新页面上,当我询问时

                  On a new page, when I ask

                  var policy_shot = localStorage.getItem( 'elephant' );
                  console.log( policy_shot );
                  
                  $('#TestScreen_mobileimage_1').src = policy_shot ;
                  

                  它记录二进制文件:

                  data:image/png;base64,iVBORw0KGgoAAAANSUhEUg ....
                  

                  但是图片没有出现.

                  1. 有没有更简单的方法?
                  2. 为什么getItem(二进制)前面是data:image/png;而不是 data:image/jpg ?
                  3. 这是它不显示的原因,还是我做错了什么?

                  推荐答案

                  1) 这是您可以在本地将图像转换为字符串的唯一方法(除了使用 FileReader,见下文).(可选)您需要使用服务器来执行此操作.

                  1) This is the only way you can convert an image into a string locally (with the excpetion of files loaded with FileReader, see below). Optionally you need to use the server to do it.

                  2) 要获取 JPEG 图像,您需要使用如下参数:

                  2) To get a JPEG image you need to use the argument like this:

                  var datauri = imgCanvas.toDataURL("image/jpeg", 0.5); //0.5 = optional quality
                  

                  不要在参数中使用 data:... ,因为这将是无效的,并且会生成默认的 PNG,正如您在结果中看到的那样.toDataURL() 只能接受一个类型,即.image/pngimage/jpeg

                  don't use data:... in the argument as that will be invalid and it will produce the default PNG as you can see in your result. toDataURL() can only take a type, ie. image/png, image/jpeg etc.

                  3)

                  如果您的图像是从不同的来源(方案、服务器...)或使用本地引用文件(file:///my/path/ 等)CORS 启动并阻止您创建数据 uri,即:图像将为空(因此不可见).

                  If your image was loaded from a different origin (scheme, server ...) or by using local referenced files (file://, /my/path/ etc.) CORS kicks in and will prevent you from creating a data-uri, that is: the image will be empty (and therefor invisible).

                  对于外部服务器,您可以通过提供 crossOrigin 属性来请求使用跨域图像的权限:

                  For external servers you can request permission to use the image from a cross-origin by supplying the crossOrigin property:

                  <img src="http://extrernalserver/...." crossOrigin="anonymous" />
                  

                  或在设置src之前通过代码(img.crossOrigin = 'anonymous';).

                  or by code (img.crossOrigin = 'anonymous';) before setting the src.

                  但是,允许或拒绝请求取决于服务器.

                  It's however up to the server to allow or deny the request.

                  如果仍然无法正常工作,您将需要通过代理加载您的图片(例如,您服务器上可以加载的页面以及外部图片并从您自己的服务器提供图片).

                  If it still doesn't work you will need to load your image through a proxy (f.ex. a page on your server that can load and the image externally and serve it from your own server).

                  或者,如果您有权访问服务器的配置,则可以添加特殊的访问允许标头(Access-Control-Allow-Origin: *,有关详细信息,请参阅下面的链接).

                  Or if you have access to the server's configuration you can add special access allow headers (Access-Control-Allow-Origin: *, see link below for more details).

                  CORS (跨域资源共享) 是一种安全机制,除此之外无法解决这些方法.

                  CORS (Cross-Origin Resource Sharing) is a security mechanism and can't be worked around other than these ways.

                  对于本地文件,您需要使用 FileReader - 这可能是一个优势,因为 FileReader 带有一个方便的方法:readAsDataURL().这使您可以直接将图像文件上传"为数据 uri,而无需通过画布.

                  For local files you will need to use FileReader - this can turn out to be an advantage as FileReader comes with a handy method: readAsDataURL(). This allow you to "upload" the image file directly as a data-uri without going by canvas.

                  在此处查看示例:
                  https://developer.mozilla.org/en-US/docs/Web/API/FileReader

                  不幸的是,您不能只从代码中选择文件 - 您需要提供输入元素或拖放区,以便用户可以选择他想要存储的文件.

                  Unfortunately you can't just pick the files from code - you will need to provide an input element or a drop zone so the user can pick the files (s)he want to store.

                  如果所有这些步骤都完成到您确实获得图像的程度,那么问题可能出在另一端,被截断的字符串太长而无法存储.

                  If all these steps are fulfilled to the degree that you actually do get an image the problem possibly is in the other end, the string being truncated being too long to store.

                  可以通过检查字符串存储前后的长度来验证是否被截断:

                  You can verify by checking the length before and after storing the string to see if it has been cut:

                  console.log(imgAsDataURL.length);
                  ... set / get ...
                  console.log(pic.length);
                  

                  其他可能性:

                  • 图像元素未正确定义.
                  • 浏览器中的错误
                  • 框架中的一个错误

                  (我想我涵盖了大部分典型的陷阱?)

                  (I think I covered most of the typical pitfalls?)

                  更新(错过了一个,有点……;-p)

                  Update (missed one, sort of.. ;-p)

                  OP 在框架中找到了一个特定的内容,我将在此处包含以供将来参考:

                  OP found a specific in the framework which I'll include here for future reference:

                  最后,问题出在 $('#TestScreen_mobileimage_1').src =policy_shot ; 我正在使用 Appery.io,但它们不支持 .src.

                  In the end, the issue was with $('#TestScreen_mobileimage_1').src = policy_shot ; I'm using Appery.io and they don't support .src.

                  它是 $('#TestScreen_mobileimage_1').attr("src", policy_shot ) ;

                  最后一点:localStorage 在存储图像方面非常有限.典型存储空间为 2.5 - 5 mb.存储的每个 char 占用 2 个字节,并且存储编码为 base-64 的 data-uri 比原始数据大 33% - 所以空间将是稀缺的.查看 Indexed DB、Web SQL 或 File API 寻找好的替代方案.

                  A final note: localStorage is very limited in regards to storing images. Typical storage space is 2.5 - 5 mb. Each char stored takes 2 bytes and storing a data-uri encoded as base-64 is 33% larger than the original - so space will be scarce. Look into Indexed DB, Web SQL or File API for good alternatives.

                  这篇关于如何将图像存储和检索到localStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)
                  How to show a running progress bar while page is loading(如何在页面加载时显示正在运行的进度条)

                      • <bdo id='49cKy'></bdo><ul id='49cKy'></ul>
                      • <i id='49cKy'><tr id='49cKy'><dt id='49cKy'><q id='49cKy'><span id='49cKy'><b id='49cKy'><form id='49cKy'><ins id='49cKy'></ins><ul id='49cKy'></ul><sub id='49cKy'></sub></form><legend id='49cKy'></legend><bdo id='49cKy'><pre id='49cKy'><center id='49cKy'></center></pre></bdo></b><th id='49cKy'></th></span></q></dt></tr></i><div id='49cKy'><tfoot id='49cKy'></tfoot><dl id='49cKy'><fieldset id='49cKy'></fieldset></dl></div>
                          <tfoot id='49cKy'></tfoot>
                            <tbody id='49cKy'></tbody>

                            <small id='49cKy'></small><noframes id='49cKy'>

                            <legend id='49cKy'><style id='49cKy'><dir id='49cKy'><q id='49cKy'></q></dir></style></legend>