[英]Image created without filetype - need filetype for compression - Angular 2 / Ionic 3
我正在嘗試壓縮客戶端的圖像(Angular 2 / Ionic 3),當我記錄由相機創建的文件時,它說:
"type":null
當它應該說"type":'image/jpeg'
。
我使用Ionic相機插件來處理照片或從照片庫中選擇一張照片,然后(我假設)創建的文件沒有類型。 我嘗試過的每個壓縮工具都存在此問題,並且我的選件用完了。 有沒有辦法更改File
對象的type
?
我使用此方法創建了一個新的Blob,並確保為其指定圖像類型:
dataURItoBlob(dataURI, callback): Promise<any> {
return new Promise((resolve, reject) => {
let byteString = atob(dataURI);
//console.log(byteString);
// separate out the mime component
//let mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
let ab = new ArrayBuffer(byteString.length);
let ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
let bb = new Blob([ab], {type: 'image/jpeg'});
resolve(bb);
})
}
我像這樣將圖像的內容讀取到base64后使用了它,並得到了圖像類型為blob的結果:
var readerZ = new FileReader();
readerZ.onload = (e) => {
let data = readerZ.result.split(',')[1];
//console.log(data);
self.dataURItoBlob(data, null).then(blob => {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.