简体   繁体   English

将图像数据设置为图像src

[英]Set image data into image src

I have a function that gets the image data on click, the image data is defined as a variable, what i want to do is to take this image data and set it into an image src, how can i do that? 我有一个函数,可以单击获取图像数据,图像数据定义为变量,我要做的就是获取此图像数据并将其设置为图像src,我该怎么做? here is my code: 这是我的代码:

  $('.export').click(function() { var imageData = $('.image-editor').cropit('export'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <img src=''> 

You can use the jQuery attr() function: 您可以使用jQuery attr()函数:

$('img').attr('src', 'data:image/png;base64,' + imageData)

See more about the function - http://api.jquery.com/attr/ 查看有关该功能的更多信息-http://api.jquery.com/attr/

Try jquery .attr() 试试jquery .attr()

Update your image element <img id='profile-pic' src=''> 更新您的图片元素<img id='profile-pic' src=''>

$('.export').click(function() {
    var imageData = $('.image-editor').cropit('export');
    $('#profile-pic').attr('src', imageData); //Change your img src to imageData
});

Update 1: For base64 更新1:对于base64

$('#profile-pic').attr('src', 'data:image/png;base64,' + imageData);

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

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