http://www.bcty365.com/content-146-3648-1.html
使用流程
彈出actionSheet
- /*點擊頭像觸發*/
- document.getElementById('headImage').addEventListener('tap', function() {
- if (mui.os.plus) {
- var a = [{
- title: "拍照"
- }, {
- title: "從手機相冊選擇"
- }];
- plus.nativeUI.actionSheet({
- title: "修改用戶頭像",
- cancel: "取消",
- buttons: a
- }, function(b) { /*actionSheet 按鈕點擊事件*/
- switch (b.index) {
- case 0:
- break;
- case 1:
- getImage(); /*拍照*/
- break;
- case 2:
- galleryImg();/*打開相冊*/
- break;
- default:
- break;
- }
- })
- }
- }, false);
拍照上傳
- //拍照
- function getImage() {
- var c = plus.camera.getCamera();
- c.captureImage(function(e) {
- plus.io.resolveLocalFileSystemURL(e, function(entry) {
- var s = entry.toLocalURL() + "?version=" + new Date().getTime();
- uploadHead(s); /*上傳圖片*/
- }, function(e) {
- console.log("讀取拍照文件錯誤:" + e.message);
- });
- }, function(s) {
- console.log("error" + s);
- }, {
- filename: "_doc/head.png"
- })
- }
從相冊選圖上傳
- //本地相冊選擇
- function galleryImg() {
- plus.gallery.pick(function(a) {
- plus.io.resolveLocalFileSystemURL(a, function(entry) {
- plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
- root.getFile("head.png", {}, function(file) {
- //文件已存在
- file.remove(function() {
- console.log("file remove success");
- entry.copyTo(root, 'head.png', function(e) {
- var e = e.fullPath + "?version=" + new Date().getTime();
- uploadHead(e); /*上傳圖片*/
- //變更大圖預覽的src
- //目前僅有一張圖片,暫時如此處理,后續需要通過標准組件實現
- },
- function(e) {
- console.log('copy image fail:' + e.message);
- });
- }, function() {
- console.log("delete image fail:" + e.message);
- });
- }, function() {
- //文件不存在
- entry.copyTo(root, 'head.png', function(e) {
- var path = e.fullPath + "?version=" + new Date().getTime();
- uploadHead(path); /*上傳圖片*/
- },
- function(e) {
- console.log('copy image fail:' + e.message);
- });
- });
- }, function(e) {
- console.log("get _www folder fail");
- })
- }, function(e) {
- console.log("讀取拍照文件錯誤:" + e.message);
- });
- }, function(a) {}, {
- filter: "image"
- })
- };
圖片上傳和壓縮
- //上傳頭像圖片
- function uploadHead(imgPath) {
- console.log("imgPath = " + imgPath);
- mainImage.src = imgPath;
- mainImage.style.width = "60px";
- mainImage.style.height = "60px";
- var image = new Image();
- image.src = imgPath;
- image.onload = function() {
- var imgData = getBase64Image(image);
- /*在這里調用上傳接口*/
- // mui.ajax("圖片上傳接口", {
- // data: {
- //
- // },
- // dataType: 'json',
- // type: 'post',
- // timeout: 10000,
- // success: function(data) {
- // console.log('上傳成功');
- // },
- // error: function(xhr, type, errorThrown) {
- // mui.toast('網絡異常,請稍后再試!');
- // }
- // });
- }
- }
- //將圖片壓縮轉成base64
- function getBase64Image(img) {
- var canvas = document.createElement("canvas");
- var width = img.width;
- var height = img.height;
- // calculate the width and height, constraining the proportions
- if (width > height) {
- if (width > 100) {
- height = Math.round(height *= 100 / width);
- width = 100;
- }
- } else {
- if (height > 100) {
- width = Math.round(width *= 100 / height);
- height = 100;
- }
- }
- canvas.width = width; /*設置新的圖片的寬度*/
- canvas.height = height; /*設置新的圖片的長度*/
- var ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0, width, height); /*繪圖*/
- var dataURL = canvas.toDataURL("image/png", 0.8);
- return dataURL.replace("data:image/png;base64,", "");
- }
總結
在使用中,我們可以發現,使用流程是非常清晰的;某種程度來說比原生iOS的使用還要簡單一些。