[英]How to make javascript image blob object in react native?
我必須像這樣制作一個圖像 blob 對象:
現在我可以在 react native 中獲取圖像 base64 url,我將其存儲在一個變量中:
const base64 = img.data;
我試過這種方式,但沒有奏效:
function b64toBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], {type: 'image/jpeg'});
}
const blob = b64toBlob(base64);
console.log(blob);
const base64 = img.data;
現在如何在 react native 中使用這個 base64 變量創建一個 blob 對象,就像上圖一樣?
我這樣解決了這個問題:
ImagePicker.openPicker({
width: 400,
height: 400,
cropping: true,
includeBase64: true,
})
.then(image => {
setImg(image);
})
.then(() => setModalVisible(prev => !prev));
const image = {
name: nn,
uri: img.path,
type: img.mime,
size: img.size,
lastModifiedDate: JSON.parse(img.modificationDate),
uid: img.modificationDate,
};
const form = new FormData();
form.append('file', image);
axios
.post('url', form, {
headers: {
Authorization: `bearer ${JSON.parse(token)}`,
},
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.