H5移动端按住图片1S保存图片


 

最近H5有个需求,就是按住图片将该图片保存在手机相册。于是就有了本文了

不说废话,上代码。

 

 1 //按住图片点击事件触发事件
 2 var timeOutEvent=0;
 3 $(function(){
 4     $("#imgId").on({
 5         touchstart: function(e){ //手指触摸
 6             timeOutEvent = setTimeout("longPress()",1000);
 7         },
 8         touchmove: function(){//上下滑动
 9             clearTimeout(timeOutEvent);
10             timeOutEvent = 0;
11         },
12         touchend: function(){ //手指拿开
13             clearTimeout(timeOutEvent);
14         }
15     })
16 });
17 function longPress(){
18     timeOutEvent = 0;
19     share()
20 }
21 
22 // js调用安卓方法保存图片  2019年10月23日15:14:00  wl
23 function share(){
24     
25     var channel = getCookie("channel");  //共享宝app缓存标识
26     if(!openStrDoor(channel)){
27         return;
28     }
29     var filePath ;  
30     var pageImg = $("#pageImg").attr("src"); //图片base64
31     if(!openStrDoor(pageImg)) {
32         cxAlert("图片信息获取有误,请稍后重试!" + pageImg);
33         return;
34     }
35     try{
36         filePath  = pageImg.replace("data:image/png;base64,",''); 
37         var params={ type: 'image', link: filePath, cbname: '', };
38         if (window.AppJSInterface) { 
39              //安卓调用保存图片方法
40              window.AppJSInterface.imageDownloadWithImageUrl(filePath) 
41         } else if (openStrDoor(window.webkit) && openStrDoor(window.webkit.messageHandlers)) {  
42              //ios调用保存图片方法
43              window.webkit.messageHandlers.imageDownloadWithImageUrl.postMessage(filePath) 
44         }  
45     }catch(err){
46         jsError(err);
47     }    
48 }    
49 
50 /* 获取共享宝app缓存标识*/
51 function getCookie(cookieName){  
52     var cookieValue="";  
53     if (document.cookie && document.cookie != '') {   
54         var cookies = document.cookie.split(';');  
55         for (var i = 0; i < cookies.length; i++) {   
56              var cookie = cookies[i];  
57              if (cookie.substring(0, cookieName.length + 2).trim() == cookieName.trim() + "=") {  
58                    cookieValue = cookie.substring(cookieName.length + 2, cookie.length);   
59                    break;  
60              }  
61          }  
62     }   
63     return cookieValue;  
64 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM