我尝试在我的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
});
}
})
}
答案 0 :(得分:2)
我遇到了同样的问题,我建议的解决方法是将文件类型放入州/省
在构造函数中
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()中,应将哑剧置于文件类型,其状态如此处实现的那样
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);
});
});
}
这是我的第一个答案
答案 1 :(得分:1)
这适用于我的情况:
userPhoto
是一个blob字符串。
<img className="pic__photo" src={"data:image/png;base64," + userPhoto} />
答案 2 :(得分:0)
目前,您要做的是将base64
转换为blob
并从获取blob
,将图片上传到 firebase 成功查询并转换为base64
以再次显示。
由于您已经在使用承诺,因此您可以检查图片是否已成功上传,然后显示相同的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
});
})