既然是解決問題,那就簡單說一下,直接上代碼!javascript
html方式使用<input type="file">在iOS上能夠直接調用照相機拍照,豎拍出來的圖片都會變成橫圖! css
思路:獲取到照片拍攝的方向角,對非橫拍的ios照片使用canvas的rotate進行角度旋轉修正。html
頁面引入exif.js 網盤分享給你吧http://pan.baidu.com/s/1geNuzGfjava
利用exif.js讀取照片的拍攝信息,詳見 http://code.ciaoca.com/javasc...ios
這裏主要用到Orientation屬性。canvas
EXIF.getData(_ImgFile, function() { //_ImgFile圖片數據 var Orientation = EXIF.getTag(this, 'Orientation'); return Orientation //Orientation返回的參數 1 、3 、6 、8 });
Orientation 參數 一、三、六、8 對應的你須要旋轉的角度app
1 0° 3 180° 6 順時針90° 8 逆時針90°
ios拍出來照片信息裏面Orientation 是6 順時針90°this
switch(Orientation){ case 6: // 旋轉90度 widthORheight=0; cvs.width = this.height * scale; cvs.height = this.width * scale; ctx.rotate(Math.PI / 2); // (0,-imgHeight) 從旋轉原理圖那裏得到的起始點 ctx.drawImage(this, 0,-this.height * scale, this.width * scale, this.height * scale ); break; case 3: // 旋轉180度 ctx.rotate(Math.PI); ctx.drawImage(this, -this.width * scale, -this.height * scale, this.width * scale, this.height * scale); break; case 8: // 旋轉-90度 widthORheight=0; cvs.width = this.height * scale; cvs.height = this.width * scale; ctx.rotate(3 * Math.PI / 2); ctx.drawImage(this, - this.width * scale, 0, this.width * scale, this.height * scale); break; }
所有代碼url
htlm:code
<input id="showImg" type="file" name="image" accept="image/*" multiple> <ul id="fileList" class="uploader-list"></ul>
css 隨意
js
// 轉換blob對象用於上傳 function dataURLtoBlob(dataurl) { var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], {type:mime}); } var imgBlobIndex=[]; //存放多張圖片的的容器。用於多張圖片上傳或者刪除多張圖片中某一張圖片, $("#showImg").change(function() { var listNunber=$('#fileList').find('img').length, Orientation = null, _this = $(this)[0], _leng = this.files.length, maxSize = 2500000,// 限制單張大小2.5M minSize=100000; //壓縮臨界 1M for (var j = 0; j < _leng; j++) { var _filelist = _this.files[j], fileType = _filelist.type, size = _filelist.size; //獲取圖片的大小 if (size < maxSize) { //獲取圖片Orientation參數 EXIF.getData(_filelist, function() { Orientation = EXIF.getTag(this, 'Orientation'); }); var fileReader = new FileReader(); fileReader.readAsDataURL(_filelist); fileReader.onload = function (event) { var result = event.target.result; //返回的dataURL var image = new Image(); image.src = result; image.onload = function () {//建立一個image對象,給canvas繪製使用 var cvs = document.createElement('canvas'); var ctx = cvs.getContext('2d'); var scale = 1; //預留壓縮比 cvs.width = this.width * scale; cvs.height = this.height * scale;//計算等比縮小後圖片寬高 if(Orientation && Orientation != 1){ switch(Orientation){ case 6: // 旋轉90度 cvs.width = this.height * scale; cvs.height = this.width * scale; ctx.rotate(Math.PI / 2); // (0,-imgHeight) 從旋轉原理圖那裏得到的起始點 ctx.drawImage(this, 0,-this.height * scale, this.width * scale, this.height * scale ); break; case 3: // 旋轉180度 ctx.rotate(Math.PI); ctx.drawImage(this, this.width * scale, -this.height * scale, this.width * scale, this.height * scale); break; case 8: // 旋轉-90度 cvs.width = this.height * scale; cvs.height = this.width * scale; ctx.rotate(3 * Math.PI / 2); ctx.drawImage(this, - this.width * scale, 0, this.width * scale, this.height * scale); break; } }else{ ctx.drawImage(this, 0, 0, cvs.width, cvs.height); } /* ctx.drawImage(this, 0, 0, cvs.width, cvs.height);*/ if(size<minSize){ //小於壓縮臨界的,壓縮0.7 var newImageData = cvs.toDataURL(fileType, 0.7); }else { //大於壓縮臨界的,根據原圖的大小動態設置壓縮比 var sca=1/(Math.sqrt([size/minSize])); // var newImageData = cvs.toDataURL(fileType, sca); } var img='<li class="list_b" data-id="'+imgid+'"><img src="'+newImageData+'" class="removeImgname'+i+'" data-id="'+imgid+'"/></li>'; //建立預覽對象 imgid++; i++; $('#fileList').append(img); //圖片預覽容器 var imgdata=dataURLtoBlob(newImageData); // 建立blob 用於上傳 imgBlobIndex.push(imgdata); //多張圖片時上傳用 }; }; }else { alert('您照片大小超過2.5M了,請更換照片') } } });