apicloud直接上传图片

function getPicture() {
    api.confirm({
      title : "提示",
      msg : "选择图片",
      buttons : ["现在照", "相册选", "取消"]
    }, function(ret, err) {
      //定义图片来源类型
      var sourceType;
      if (1 == ret.buttonIndex) {/* 打开相机*/
        sourceType = "camera";
        openPicture(sourceType);
      } else if (2 == ret.buttonIndex) {
        sourceType = "album";
        openPicture(sourceType);
      } else {
        return;
      }
      });
 }
  //  选取图片
function openPicture(sourceType) {
  var q = 100;
  //获取一张图片
  api.getPicture({
    sourceType : sourceType,
    encodingType : 'png',
    mediaValue : 'pic',
    //返回数据类型,指定返回图片地址或图片经过base64编码后的字符串
    //base64:指定返回数据为base64编码后内容,url:指定返回数据为选取的图片地址
    destinationType : 'base64',
    //是否可以选择图片后进行编辑,支持iOS及部分安卓手机
    allowEdit : false,
    //图片质量,只针对jpg格式图片(0-100整数),默认值:50
    quality : q,
    //                targetWidth : 100,
    //                targetHeight : 1280,
    saveToPhotoAlbum : true
  }, function(ret, err) {
    if (ret) {
      //  data:"fs://",                //图片路径
      //  base64Data:"",          //base64数据,destinationType为base64时返回
      var img_url = ret.data;
      if (img_url != "") {
          function getBase64Image(img1) {
            var cvs = document.createElement("canvas");
            var width = img1.width;
            height = img1.height;
            var scale = width / height;
            cvs.width = width;
             cvs.height = height;
            var ctx = cvs.getContext("2d");
            ctx.drawImage(img1, 0, 0,width,height,0,0,width,height);
            var src = cvs.toDataURL("image/jpeg",0.7);
            return src;
          }
        var image=new Image();
        $('#imgUp').show();
        $('.imgtext').hide();
        $('#imgUp').attr('src',img_url);
        image.src = $('#imgUp').attr('src');
        image.onload=function(){//转base64图片
          var base64 = getBase64Image(image);
          localStorage.base64=base64;
          var imgmsg = {UpType:'HandheldPhoto',Vid:'',Uid:Uid,Position:'',Base64:localStorage.base64,Uid:Uid,Client_Id:Client_Id};
          Get_request(120102, imgmsg, 'post', 1);//1,编号  2,要传送的值  3,请求方式  4,请求地址
        };
      }
    }
  });
};

 

posted @ 2017-08-28 11:45  永往  阅读(2045)  评论(0编辑  收藏  举报