1

I am working on a react native application where we let users pick images and upload them to server. I am using react-native-image-crop-picker library version 0.33.2.

I have an Image which shows as 5,979,564 bytes (6 MB on disk). When I select it from above library with 100% image quality it shows as 13167536 bytes whereas when I select the same image on Android it shows around 5979564 bytes.

This is my code for validating image size which is failing due to the above issue:


const LocalizationConstants = getTranslations();

let imagesArray = [...images];

let sizeInBytes = 0;

if (imagesArray?.length > this.state.maxFilesAllowed) {
  imagesArray = imagesArray?.slice?.(0, this.state.maxFilesAllowed);

  Snackbar.show({
    text: LocalizationConstants.SNACKBAR.IMAGES_COUNT_EXCEEDED(
      this.state.maxFilesAllowed,
    ),
    duration: Snackbar.LENGTH_LONG,
  });
}

imagesArray?.forEach?.((im) => {
  sizeInBytes += Number(im.size || 0);
});

const totalSize =
  imagesArray?.length * GENERIC_CONSTANTS.MAX_INDIVIDUAL_ATTACHMENT_SIZE // 10 MB per file;

if (sizeInBytes / Math.pow(1024, 2) > totalSize) {
  Snackbar.show({
    text: LocalizationConstants.SNACKBAR.IMAGE_SIZE_EXCEEDED(
      GENERIC_CONSTANTS.MAX_INDIVIDUAL_ATTACHMENT_SIZE,
    ),
    duration: Snackbar.LENGTH_LONG,
  });
  return false;
}
return true;

EDIT:

Image in original size can be found here: https://github.com/ivpusic/react-native-image-crop-picker/issues/1931

Waleed
  • 1,097
  • 18
  • 45

0 Answers0