如何显示反应本机的图像 blob



我尝试在我的反应原生应用程序中显示图像。我已将照片上传到 我的火力基地存储和它的工作。然后我尝试显示来自Firebase Storage的图像,但该文件是blob。我想将 blob 转换为图像,但我不知道语法。也许你们中的一个可以帮助我解决这个问题。

我应该在哪里放置RNFetch blob?我试图将组件放置在组件中,但它显示错误。

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
            });
          }
      })
    }

我遇到了同样的问题,我提出的解决方法是将文件类型放在状态中

在构造函数中

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);
            });
    });
}

顺便说一句,这是我的第一个答案

这适用于我的情况:

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
                });
            })

相关内容

  • 没有找到相关文章

最新更新