请选择 进入手机版 | 继续访问电脑版
帖子
帖子
用户
博客
课程
显示全部楼层
20
帖子
0
勋章
6613
Y币

UIPhotoViewer 模块分享,添加自定义按钮示例

[复制链接]
发表于 2019-12-26 13:36:36
模块UIPhotoViewer
文档连接:https://docs.apicloud.com/Client-API/Func-Ext/UIPhotoViewer
概述
UIPhotoViewer 是一个图片浏览器,是photoBrowser模块的升级版,支持gif图的浏览,支持单张、多张图片查看的功能,可放大缩小图片(android,gif暂不支持缩放),支持本地和网络图片资源。若是网络图片资源则会被缓存到本地,缓存到本地上的资源可以通过 clearCache 接口手动清除。同时本模块支持横竖屏显示,在本app支持横竖屏的情况下,本模块底层会自动监听当前设备的位置状态,自动适配横竖屏以展示图片。使用此模块开发者看实现炫酷的图片浏览器。
模块使用攻略
开发者使用此模块时可以用 frame 的形式打开并添加到主窗口上,该 frame 不可设置位置和大小,其宽高默认和当前设备屏幕的宽高相同。模块打开后可再 open 一个自定义的 frame 贴在本模块上,从而实现自定义图片浏览器样式和功能。需要适配横竖屏时,开发者可通过api对象监听当前设备的位置状态,以改变自己自定义的 frame 的横竖屏展示,而图片的展示模块内部会自动适配横竖屏,最终实现了整个浏览器的横竖屏配置。在本模块的 open 接口内可以获取图片的下载状态,通过 getImage 接口获取目标图片在本地的绝对路径,以实现保存到系统相册的功能。详情请参考模块接口参数说明。
api调用: execScript 或者 sendEvent 来进行 夸frame 操作实现
注意:android编译说明
  • 使用升级环境编译
  • 最低版本支持到4.0及4.0以上
运行截图 (帖子最后有大图


        

主要代码 (两个frame页面)
主页面
  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  6.     <title>title</title>
  7.     <link rel="stylesheet" type="text/css" href="../css/api.css" />
  8.     <style>
  9.         body {}
  10.     </style>
  11. </head>

  12. <body>

  13. </body>
  14. <script type="text/javascript" src="../script/api.js"></script>
  15. <script type="text/javascript">
  16.     var UIPhotoViewer = null;
  17.     apiready = function() {
  18.         UIPhotoViewer = api.require('UIPhotoViewer');
  19.         setTimeout(function() {
  20.             openP();
  21.         }, 1000);
  22.     };

  23.     function getIndexU() {
  24.         UIPhotoViewer.getIndex(function(ret, err) {
  25.             if (ret) {
  26.                 alert( '当前选中为第 ' + (ret.index + 1) + ' 张');
  27.             } else {
  28.                 alert(JSON.stringify(err));
  29.             }
  30.         });
  31.     }
  32.     function closeView() {
  33.       UIPhotoViewer.close();
  34.     }

  35.     function openP() {
  36.         UIPhotoViewer.open({
  37.             images: [
  38.                 'http://img1.imgtn**.**/it/u=3962320828,1701933979&fm=26&gp=0.jpg',
  39.                 'http://img0.imgtn**.**/it/u=2943994938,4115220466&fm=26&gp=0.jpg',
  40.                 'http://img2.imgtn**.**/it/u=4200942165,215200833&fm=26&gp=0.jpg',
  41.                 'http://img4.imgtn**.**/it/u=4058349123,3836499281&fm=26&gp=0.jpg',
  42.                 'http://img0.imgtn**.**/it/u=4137105188,1979966924&fm=26&gp=0.jpg',
  43.                 'https://community.apicloud.com/bbs/data/attachment/forum/201912/25/214216rsas73lal6p42nim.gif'
  44.             ],
  45.             placeholderImg: 'widget://image/dog.png',
  46.             bgColor: '#000'
  47.         }, function(ret, err) {
  48.             if (ret) {
  49.                 if (ret.eventType == 'change') {
  50.                     api.sendEvent({
  51.                         name: 'myEvent',
  52.                         extra: {
  53.                             key1: ret.index + 1,
  54.                         }
  55.                     });
  56.                 }
  57.             } else {
  58.                 alert(JSON.stringify(err));
  59.             }
  60.         });
  61.         setTimeout(function() {
  62.             api.openFrame({
  63.                 name: 'photoView_frame',
  64.                 url: './photoView_frame.html',
  65.                 rect: {
  66.                     x: 0,
  67.                     y: 0,
  68.                     w: 'auto',
  69.                     h: api.safeArea.top + 60
  70.                 },
  71.                 pageParam: {
  72.                     name: 'test'
  73.                 },
  74.                 bounces: false,
  75.                 bgColor: 'rgba(0,0,0,0)',
  76.                 vScrollBarEnabled: true,
  77.                 hScrollBarEnabled: true
  78.             });
  79.         }, 800);

  80.     }
  81. </script>

  82. </html>
复制代码
header frame
  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  6.     <title>title</title>
  7.     <!-- <link rel="stylesheet" type="text/css" href="../css/api.css"/> -->
  8.     <style>
  9.         body {
  10.             margin: 0;
  11.             border: 0;
  12.             padding: 0;
  13.             /*font-style:normal;*/
  14.         }

  15.         header {
  16.             width: 100%;
  17.             height: 60px;
  18.             background-color: #EEEEEE;
  19.         }

  20.         .right {
  21.             float: left;
  22.             width: 20%;
  23.             height: 60px;
  24.         }

  25.         .page {
  26.             float: left;
  27.             width: 60%;
  28.             height: 60px;
  29.         }

  30.         .left {
  31.             float: left;
  32.             width: 20%;
  33.             height: 60px;
  34.         }

  35.         .txt {
  36.             text-align: center;
  37.             line-height: 60px;
  38.             font-size: 20px;
  39.         }
  40.     </style>
  41. </head>

  42. <body>
  43.     <header>
  44.         <div class="right txt" onclick="fnBack()">
  45.             返回
  46.         </div>
  47.         <div id="paget" class="page txt">
  48.             第1张
  49.         </div>
  50.         <div class="left txt" onclick="fnOk()">
  51.             确定
  52.         </div>
  53.     </header>
  54.     <nav>

  55.     </nav>
  56.     <footer>

  57.     </footer>
  58. </body>
  59. <script type="text/javascript" src="../script/api.js"></script>
  60. <script type="text/javascript">
  61.     apiready = function() {
  62.         var paget = null;
  63.         paget = $api.byId('paget');
  64.         $api.dom('header').style.paddingTop = api.safeArea.top + 'px';
  65.         api.addEventListener({
  66.             name: 'myEvent'
  67.         }, function(ret, err) {
  68.             if (ret) {
  69.                 var page = ret.value.key1;
  70.                 $api.html(paget, '第 ' + page + ' 张');
  71.             } else {
  72.                 alert(JSON.stringify(err));
  73.             }
  74.         });

  75.     };

  76.     function fnBack() {
  77.       api.execScript({
  78.           frameName: 'page1',
  79.           script: 'closeView()'
  80.       });
  81.       api.closeFrame();
  82.     }

  83.     function fnOk() {
  84.         api.execScript({
  85.             frameName: 'page1',
  86.             script: 'getIndexU()'
  87.         });
  88.     }
  89. </script>

  90. </html>
复制代码




本帖子中包含更多资源,您需要 登录 才可以下载或查看,没有帐号?立即注册

X
20
帖子
0
勋章
6613
Y币
更新:  通过api.getPicture接口获取图片路径 然后预览
  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  6.     <title>title</title>
  7.     <link rel="stylesheet" type="text/css" href="../css/api.css" />
  8.     <style>
  9.         body {}
  10.     </style>
  11. </head>

  12. <body>

  13. </body>
  14. <script type="text/javascript" src="../script/api.js"></script>
  15. <script type="text/javascript">
  16.     var UIPhotoViewer = null;
  17.     apiready = function() {
  18.         UIPhotoViewer = api.require('UIPhotoViewer');
  19.         setTimeout(function() {
  20.             getPictureP();
  21.         }, 1000);
  22.     };

  23.     function getIndexU() {
  24.         UIPhotoViewer.getIndex(function(ret, err) {
  25.             if (ret) {
  26.                 alert( '当前选中为第 ' + (ret.index + 1) + ' 张');
  27.             } else {
  28.                 alert(JSON.stringify(err));
  29.             }
  30.         });
  31.     }
  32.     function closeView() {
  33.       UIPhotoViewer.close();
  34.     }
  35.     function getPictureP() {
  36.       api.getPicture({
  37.           sourceType: 'library',
  38.           encodingType: 'jpg',
  39.           mediaValue: 'pic',
  40.           destinationType: 'url',
  41.           allowEdit: false,
  42.           quality: 50,
  43.           targetWidth: 600,
  44.           targetHeight: 900,
  45.           saveToPhotoAlbum: false
  46.       }, function(ret, err){
  47.           if(ret){
  48.                alert(JSON.stringify(ret));
  49.                var path = ret.data;
  50.                openP( path );
  51.           }else{
  52.                alert(JSON.stringify(err));
  53.           }
  54.       });

  55.     }
  56.     function openP( path ) {
  57.         UIPhotoViewer.open({
  58.             images: [
  59.               path
  60.                 // 'http://img1.imgtn**.**/it/u=3962320828,1701933979&fm=26&gp=0.jpg',
  61.                 // 'http://img0.imgtn**.**/it/u=2943994938,4115220466&fm=26&gp=0.jpg',
  62.                 // 'http://img2.imgtn**.**/it/u=4200942165,215200833&fm=26&gp=0.jpg',
  63.                 // 'http://img4.imgtn**.**/it/u=4058349123,3836499281&fm=26&gp=0.jpg',
  64.                 // 'http://img0.imgtn**.**/it/u=4137105188,1979966924&fm=26&gp=0.jpg',
  65.                 // 'https://community.apicloud.com/bbs/data/attachment/forum/201912/25/214216rsas73lal6p42nim.gif'
  66.             ],
  67.             placeholderImg: 'widget://image/dog.png',
  68.             bgColor: '#000'
  69.         }, function(ret, err) {
  70.             if (ret) {
  71.                 if (ret.eventType == 'change') {
  72.                     api.sendEvent({
  73.                         name: 'myEvent',
  74.                         extra: {
  75.                             key1: ret.index + 1,
  76.                         }
  77.                     });
  78.                 }else if (ret.eventType == 'click') {
  79.                   UIPhotoViewer.close();
  80.                 }
  81.             } else {
  82.                 alert(JSON.stringify(err));
  83.             }
  84.         });
  85.         setTimeout(function() {
  86.             api.openFrame({
  87.                 name: 'photoView_frame',
  88.                 url: './photoView_frame.html',
  89.                 rect: {
  90.                     x: 0,
  91.                     y: 0,
  92.                     w: 'auto',
  93.                     h: api.safeArea.top + 60
  94.                 },
  95.                 pageParam: {
  96.                     name: 'test'
  97.                 },
  98.                 bounces: false,
  99.                 bgColor: 'rgba(0,0,0,0)',
  100.                 vScrollBarEnabled: true,
  101.                 hScrollBarEnabled: true
  102.             });
  103.         }, 800);

  104.     }
  105. </script>

  106. </html>
复制代码
22
帖子
1
勋章
1万+
Y币
感谢分享~
50
帖子
2
勋章
1327
Y币
学到了
4
帖子
0
勋章
6509
Y币
yxWin 发表于 2020-1-7 15:04
更新:  通过api.getPicture接口获取图片路径 然后预览

我最近尝试在IOS中调用这个模块,但是在ios中无法预览网络图片,一直不显示
示例:数组["https://www**.**/db/tupianxinxibiao/b4/86/dd45/c6dc/c841/7363/d7c0/8827/7722/public/tp/tp.jpg","https://www**.**/db/tupianxinxibiao/ec/dc/15bd/f067/0b85/ceba/4a39/668d/18d9/public/tp/tp.jpg","https://www**.**/db/tupianxinxibiao/b4/86/dd45/c6dc/c841/7363/d7c0/8827/7722/public/tp/tp.jpg","https://www**.**/db/tupianxinxibiao/ec/dc/15bd/f067/0b85/ceba/4a39/668d/18d9/public/tp/tp.jpg","https://www**.**/db/tupianxinxibiao/b4/86/dd45/c6dc/c841/7363/d7c0/8827/7722/public/tp/tp.jpg","https://www**.**/db/tupianxinxibiao/ec/dc/15bd/f067/0b85/ceba/4a39/668d/18d9/public/tp/tp.jpg"]  我发现使用其他的图片在ios里就可以正常预览,但是就这几张只在安卓手机中可以ios手机的不行,之前还尝试了只使用一张这里边的好像还是不行;但是创建方式的话都是一样的
12
帖子
0
勋章
75
Y币
感谢分享
12
帖子
0
勋章
72
Y币
yxWin 发表于 2020-1-7 15:04
更新:  通过api.getPicture接口获取图片路径 然后预览

这个ios中 打开的 openFrame  状态栏被遮住了  试了好几种方式  还是被遮住
1
帖子
0
勋章
16
Y币
UIPhotoViewer,调用后报错,null
您需要登录后才可以回帖 登录

本版积分规则