[英]How to display image blob for react native
我尝试在我的react-native应用程序中显示图像。 我已将照片上传到我的Firebase存储中,并且它正常运行。 然后我尝试从Firebase存储显示我的图像,但该文件是blob。 我想将blob转换为图像,但我不知道语法。 你可以帮助我解决这个问题。
我应该在哪里放置RNFetch blob? 我试过放在componentWillMount里面,它显示错误。
uploadImage(uri, mime = 'application/octet-stream') {
return new Promise((resolve, reject) => {
const uploadUri = uri
let uploadBlob = null
const imageRef = firebase.storage().ref('profileImg').child(`${this.state.user.uid}/Profile Image - ${this.state.user.uid}`)
fs.readFile(uploadUri, 'base64')
.then((data) => {
return Blob.build(data, { type: `${mime};BASE64` })
})
.then((blob) => {
uploadBlob = blob
return imageRef.put(blob, { contentType: mime })
})
.then(() => {
uploadBlob.close()
return imageRef.getDownloadURL()
})
.then((url) => {
console.log(url)
resolve(url)
})
.catch((e) => {
console.log(e)
reject(e)
})
})
}
getProfileImage(){
let options = {
title: 'Choose Photo',
storageOptions: {
skipBackup: true,
path: 'images'
}
};
ImagePicker.showImagePicker(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
}
else if (response.error) {
console.log('ImagePicker Error: ', response.error);
}
else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
}
else {
let image_uri = { uri: 'data:image/jpeg;base64,' + response.data };
this.uploadImage(response.uri)
.then((url) => {
console.log('uploaded')
this.setState({
image_uri: url,
});
})
.catch((error) => console.log(error))
firebase.auth().currentUser.updateProfile({
photoURL: this.state.image_uri
});
}
})
}
我遇到了同样的问题,我实现的解决方法是将文件类型放在State中
在构造函数中
constructor() {
super();
// we will change it to fill the image file type from response
this.state = {
fileType: ""
};
}
在getImage()函数中我们应该改变状态以放置文件类型
getImage() {
ImagePicker.showImagePicker(options, response => {
console.log("Response = ", response);
this.setState({
fileType: response.type
});
......... the rest of the code
在uploadImage()中,您应该将mime设置为此处实现的fileType状态
uploadImage(uri, mime = this.state.fileType) {
return new Promise((resolve, reject) => {
const uploadUri =
Platform.OS === "ios" ? uri.replace("file://", "") : uri;
let uploadBlob = null;
const imageRef = firebase
.storage()
.ref("images/")
.child(Date.now());
fs.readFile(uploadUri, "base64")
.then(data => {
return Blob.build(data, { type: `${mime};BASE64` });
})
.then(blob => {
uploadBlob = blob;
var metadata = {
contentType: mime
};
return imageRef.put(uri, metadata);
})
.then(() => {
uploadBlob.close();
return imageRef.getDownloadURL();
})
.then(url => {
resolve(url);
})
.catch(error => {
reject(error);
});
});
}
这是我的第一个答案BTW
这适用于我的情况:
userPhoto
是一个blob字符串。
<img className="pic__photo" src={"data:image/png;base64," + userPhoto} />
现在,您要做的是将base64
转换为blob
并将blob
从成功查询中获取并转换为base64
以再次显示它,从而将图像上传到firebase 。
由于您已经在使用承诺,因此您可以检查图像是否已成功上传 ,然后显示相同的base64
否则显示original data
。
// Default Image data
let image_uri = { uri: 'data:image/jpeg;base64,' + response.data };
this.uploadImage(response.uri)
.then((url) => {
// Success, replace with base64 you've got
console.log('uploaded')
this.setState({
image_uri
});
})
.catch(error => {
this.setState({
image_uri: // SOME DEFAULT IMAGE VALUE
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.