将图像存储在 HTML5 local Storage onClick
Store images in HTML5 local Storage onClick
我目前正在使用 jQuery Mobile & PhoneGap 创建一个应用程序作为副项目。此应用程序将显示多张图片,并让用户有机会收藏 he/she 喜欢的图片。一旦用户点击按钮收藏图像,该图像将显示在收藏夹页面上 (favorite.html)。我在使用本地存储合并此任务时遇到问题。甚至可以将图像存储和检索到另一个页面吗?任何帮助都会很棒。如果您需要视觉效果,下面是我的 jsfiddle 的 link。
<!-- 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>
首先你必须确定应该存储什么图像,你可以看到图像的自定义属性称为数据名称,按钮的其他属性称为数据图像,两者具有相同的内容:
<!-- 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.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);
});
其他想法是只使用 vanilla 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")
我目前正在使用 jQuery Mobile & PhoneGap 创建一个应用程序作为副项目。此应用程序将显示多张图片,并让用户有机会收藏 he/she 喜欢的图片。一旦用户点击按钮收藏图像,该图像将显示在收藏夹页面上 (favorite.html)。我在使用本地存储合并此任务时遇到问题。甚至可以将图像存储和检索到另一个页面吗?任何帮助都会很棒。如果您需要视觉效果,下面是我的 jsfiddle 的 link。
<!-- 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>
首先你必须确定应该存储什么图像,你可以看到图像的自定义属性称为数据名称,按钮的其他属性称为数据图像,两者具有相同的内容:
<!-- 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.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);
});
其他想法是只使用 vanilla 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")