将图像存储在HTML5本地Storage onClick

时间:2015-11-18 04:16:37

标签: javascript html5 jquery-mobile local-storage favorite

我目前正在使用jQuery Mobile& amp;创建一个应用程序作为副项目PhoneGap的。此应用程序将显示多个图像,并让用户有机会喜欢他/她喜欢的图像。一旦用户点击按钮以收藏图像,该图像将显示在收藏页面(favorite.html)上。我无法使用本地存储来合并此任务。甚至可以将图像存储和检索到另一个页面?任何帮助都会很棒。如果你需要一个视觉,下面是我的jsfiddle的链接。

JSFiddle Demo

<!-- Page One -->
      <div data-role="page" id="one">
        <div data-role="header">
          <h1>Page 1</h1>
          </div>
            <div data-role="main">
                <img src="https://www.petfinder.com/wp-content/uploads/2012/11/140272627-grooming-needs-senior-cat-632x475.jpg" style="width: 100px;"/>
                <br />
                <button id="favcat">Click Here to Favorite Cat Image!</button>
                <br />
                <br />
                <img src="http://www.dogslovewagtime.com/wp-content/uploads/2015/07/Dog-Pictures.jpg" style="width: 100px;" />
                <br />
                <button id="favdog">Click Here to Favorite Dog Image!</button>

          </div>
</div>

          <!--Page Two -->
           <div data-role="page" id="two">
        <div data-role="header">
          <h1>Page 2: Stored and Retrieved Images Display On This Page Once Clicked</h1>
               </div>

            <div data-role="main">


          </div>
</div>

2 个答案:

答案 0 :(得分:1)

首先,您必须确定应存储的图像,您可以看到名为data-name的图像的自定义属性,以及名为data-image的按钮的其他属性,两者都具有相同的内容:

<!-- Page One -->
            <img data-name="catImage" src="https://www.petfinder.com/wp-content/uploads/2012/11/140272627-grooming-needs-senior-cat-632x475.jpg" style="width: 100px;"/>
            <br />
            <button class="btn-add-image" data-image="catImage">Click Here to Favorite Cat Image!</button>

现在,当用户点击图片按钮时,首先必须获取图片:

var myImageName = $(this).attr("data-image");
var myImage = $('img[data-name="' + myImageName + '"]');

你应该得到图像base64字符串

//Create a Canvas
var myCanvas = document.createElement("canvas");

//Set width and height
myCanvas.width = myImage.width();
myCanvas.height = myImage.height();

//Draw imagem
var myContext = myCanvas.getContext("2d");
myContext.drawImage(myImage, 0, 0);

//And finally get canvas64 string
var base64 = myCanvas.toDataURL("image/jpeg"); // if it's a png uses image/png

现在将其保存在localStorage:

localStorage.setItem("image", base64);

现在恢复第二页 - 第二页HTML:

<!--Page Two -->
    <div data-role="page" id="two">
        <h1>Page 2: Stored and Retrieved Images Display On This Page Once Clicked</h1>
    </div>

    <div data-role="main" id="restoredImage">

    </div>

第二页JavaScript:

$(document).ready(function() {
    var img = document.createElement("img");
    img.src = localStorage.getItem("image");
    $("#restoredImage").html(img); 
});

答案 1 :(得分:0)

其他想法只使用香草JS

第一页 - 保存在localStorage:

var img = new Image,
canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d"),
src = "http://example.com/image"; // insert image url here

img.crossOrigin = "Anonymous";


canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage( img, 0, 0 );
localStorage.setItem( "savedImageData", canvas.toDataURL("image/png") );

第二页 - 从localStorage恢复:

localStorage.getItem("savedImageData")

JSBin示例:https://jsbin.com/hewapewoxe/edit?html,js,console