var pickSuccess,//加载成功后回调 len = 0,//上传图片个数 f = [],//图片路径 n = 0;//控制循环加载变量 // 弹出选择照片方式 function showActionSheet(num) { plus.nativeUI.actionSheet({ cancel: "取消", buttons: [{ title: "从相册选择" }, { title: "拍照" }] }, function(event) { if (event.index == 1) { getGallery(num); } else if (event.index == 2) { getCamera(); } } ); }; //获取相册 function getGallery(num) { plus.gallery.pick(function(e) { //scb:SuccessCallBack len = e.files.length; f = e.files; n= 0; loadImg(); }, function(ecb) { //ecb:ErrorCallBack }, { filename: "_doc/gallery/", filter: "image", multiple:true, maximum:num||9, system:false, onmaxed:function(){ plus.nativeUI.alert('最多只能选择9张图片'); } }); } //加载多张图片 function loadImg() { Promise.all([handlePickSucc(f[n])]).then(function (res) { n++; if (res[0].status == 200) { if(pickSuccess && $.isFunction(pickSuccess)) { pickSuccess(res[0].urlSucc); } if (n < len) { loadImg(); } } else { mui.toast(res[0].msg); } }); } //图片压缩 function handlePickSucc(srcUrl) { return new Promise(function (resolve, reject) { var lastIdx = srcUrl.lastIndexOf("/"), imgName = srcUrl; if(lastIdx>-1) { imgName = srcUrl.slice(lastIdx+1); } plus.zip.compressImage({ src: srcUrl, dst: "_doc/compressImg/"+imgName, quality: 20, overwrite: true }, function(succ) { var urlSucc = succ.target; resolve({ status: 200, urlSucc: urlSucc }); }, function(err) { resolve({ status: -1, msg: "压缩失败: " + err.message }); }); }); } //加载拍照图片 function loadCameraPic(fileUrl) { Promise.all([handlePickSucc(fileUrl)]).then(function (res) { if (res[0].status == 200) { if(pickSuccess && $.isFunction(pickSuccess)) { pickSuccess(res[0].urlSucc); } } else { mui.toast(res[0].msg); } }); } // 调用系统摄像头 function getCamera() { var cmr = plus.camera.getCamera(); cmr.captureImage(function(path) { /** * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件 * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL */ plus.io.resolveLocalFileSystemURL(path, function(entry) { /* * 将获取目录路径转换为本地路径URL地址 * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL */ var fileUrl = entry.toLocalURL(); loadCameraPic(fileUrl); }); }, function(error) { if(error.code==11 && error.message == "null") { plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。"); } // mui.toast(error.message) }, { filename: "_doc/camera/", index: 1 //ios指定主摄像头 }); } window.getAutoRecCompressImageLocalPath = function(cb,num) { pickSuccess = cb; showActionSheet(num); }