简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM