ravi
ravi

Reputation: 370

how to reduce the size of a base64 image getting from react-native-image-picker to save it in firebase using react native

I trying to save an image base64 string getting from react-native-image-picker to firebase. it is working fine for some images but for images, it getting an issue like giving below.

```error: Reference.push failed: first argument contains a string greater than 10485760 utf8 bytes in property```

by getting an issue I am trying to reduce the base64 string size. but it is not working.

    selectImage = () => {
        ImagePicker.showImagePicker(options, (response) => {
            if (response.didCancel) {
            } else if (response.error) {
            } else if (response.customButton) {
            } else {
                this.setState({ avatarSource:response.data }, () => {
                  //this.base64toBlob(response.data,"base64")
                  db.ref('/Images/Details').push({
                    avatarSource: this.state.avatarSource,
                    name: this.state.name,
                    tag: this.state.selectItem
                });
                Alert.alert('you are successfully Register');
                 
                });
            }
        });
    }

Upvotes: 0

Views: 3663

Answers (2)

Ravina Vaishnav
Ravina Vaishnav

Reputation: 86

You can pass "quality: 0.5" to options and quality value can be 0 to 1

options = {            
      quality: 0.5
     };
ImagePicker.showImagePicker(options, (response) => {
   // write code
})

Upvotes: 4

Thomas Martin
Thomas Martin

Reputation: 686

to get the base64 data with the image uri;

import {
  Image,
  ImageStore,
  ImageEditor,
} from 'react-native';

Image.getSize(image, (width, height) => {
  let imageSettings = {
    offset: { x: 0, y: 0 },
    size: { width: width, height: height }
  };
  ImageEditor.cropImage(image, imageSettings, (uri) => {
    ImageStore.getBase64ForTag(uri, (data) => {
      // data == base64 encoded image
    }, e => console.warn("getBase64ForTag: ", e))
  }, e => console.warn("cropImage: ", e))
})

Upvotes: 0

Related Questions