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