Nativescript: Camera module: toBase64String(...) returns null

Created on 21 Jun 2016  路  6Comments  路  Source: NativeScript/NativeScript

Hello,

I am testing on an iOS device and I am trying to convert the picture taken into a Base 64 string so I can submit it to the server for processing. However the toBase64String() is returning null. Here is my code:

cameraModule.takePicture().then(function (img) {
        try {
            var imageData = img.toBase64String('image/png');
            //  imageData is null //
           ...
        } catch (e) {
            alert(e);
        }         
    });

This code seems to work fine on Android.

Am I doing anything wrong here?

Thank you.

question

All 6 comments

@dpdragnev
I think is should be img.toBase64String(enums.ImageFormat.png, 100);

I am doing something similar and that works.

Hi @dpdragnev

As @Daxito pointed out one way is to use the NativeScript enumerations module.

var enums = require("ui/enums");

img.toBase64String(enums.ImageFormat.png);

or to use directly the format with

img.toBase64String("png");

The second argument (quality) is optional and if not passed will be 100 by default

Thank you @Daxito and @NickIliev. I am testing it now.

For some reason, I could not make it work with img.toBase64String("png"); but img.toBase64String("jpeg"); worked.

Thanks for the help.

@dpdragnev The Android camera is taking pictures in jpeg format by default so this is the expected format to convert. (however Screenshots for most Android deveices are in png)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings