JPEG Compression Not Working - ImageSource to Base64 String


#1

Hi everyone :slight_smile:

I am converting an imageSource into a base64 encoded string as follows:

var photoEnc64String = photo.toBase64String("jpg", 30);

This line of code only partially works; it generates a base64 encoding, but no matter what the quality number I enter (0-100), it always uses the maximum quality setting (100). Does anyone know why I might be having this issue?

Here is the doc for toBase64String() : https://docs.nativescript.org/api-reference/classes/image_source.imagesource#tobase64string

Thanks for the help!
Nat :smiley:


#2

May I know how do you verify that the result string is always encoded with maximum quality?


#3

Hi! Thanks for replying :smiley:

If I don’t pass a quality argument, the encoding defaults to maximum quality, as described in the docs. When I pass in any quality parameter from 1-100, the output string takes up the same amount of storage as if i didn’t include it (in my case it was about 26 MB for a 3000x2000 image, if i remember correctly). This issue was also verified by the lack of compression artefacts when using a very low quality setting.

I found a workaround by using the Bitmap Factory plugin that has a similar encoding function, and it works in the plugin, but the one that comes with Nativescript doesn’t.

Nat


#4

@natanijelvasic is this on iOS? If it is I am also having the same problem. The toBase64String(“jpg”, 0) works but the rest doesn’t. Android works fine.


#5

Yeah, this is on iOS, but i don’t think I have tested on Android. The Bitmap Factory plugin is quite good though, and it has the fully functional encoding function (works on iOS and Android). https://www.npmjs.com/package/nativescript-bitmap-factory


#6

We have the same problem then. It is better if we open an issue on github for this. Can you make one for it?


#7

I opened an issue here: https://github.com/NativeScript/NativeScript/issues/6147

This is actually my first GtHub issue so feel free to tell me if anything needs to be added! XD

Nat


#8

I’ve mentioned having the same issue on it. They might be able to fix this issue for the next tns-core-modules update.