question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageSourceModule method toBase64String not working (both iOS and Android)

See original GitHub issue

On line 140 of image-source-android.js this.android.compress(targetFormat, quality, base64Stream); I’m getting this error this.android.compress is not a function. I’m using the nativescript-camera to take a picture and then save it to base64 string. Also, the image is saving to my photos even when I set saveToGallery: false.

Here my code:

cameraModule.takePicture({
        width: 1280, height: 720, keepAspectRatio: false, saveToGallery: false
    }).then(function (imageAsset) {
        console.log("Result is an image asset instance");
        viewModel.set("BoardingPassSource", imageAsset);
        var image = ImageSourceModule.fromNativeSource(imageAsset);
        var base64 = image.toBase64String("jpeg");
        // var image = new imageModule.Image();
        // image.imageSource = imageAsset;
        // image.toBase64String("jpeg");
        viewModel.set("base64String", base64);
    }).catch(function (err) {
        console.log("Error -> " + err.message);
    });

package.json

{
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "readme": "NativeScript Application",
  "repository": "<fill-your-repository-here>",
  "nativescript": {
    "id": "org.nativescript.****",
    "tns-android": {
      "version": "2.5.0"
    }
  },
  "dependencies": {
    "nativescript-background-http": "^2.4.2",
    "nativescript-barcodescanner": "^2.3.3",
    "nativescript-camera": "0.0.8",
    "nativescript-drawingpad": "^1.1.2",
    "nativescript-drop-down": "^1.5.1",
    "nativescript-loading-indicator": "^2.2.2",
    "nativescript-telerik-ui": "^1.5.1",
    "nativescript-theme-core": "^1.0.2",
    "nativescript-timedatepicker": "^1.1.0",
    "tns-core-modules": "^2.5.0"
  },
  "devDependencies": {
    "babel-traverse": "6.21.0",
    "babel-types": "6.21.0",
    "babylon": "6.14.1",
    "lazy": "1.0.11"
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
emmanuel128commented, Apr 28, 2017

I found another alternative!

cameraModule.takePicture({\
        width: 1280, height: 720, keepAspectRatio: false, saveToGallery: false
    }).then(function (imageAsset) {
        imageAsset.getImageAsync(function (res){
            console.log(res);
            viewModel.set("BoardingPassSource", imageSource); 
            var image = ImageSourceModule.fromNativeSource(imageSource);
            viewModel.set("base64String", image.toBase64String('png'));
            // var stream;
            // var comp = image.android.compress("png", 70, stream);
            console.log("image selected"); 
        });
    }).catch(function (err) {
        console.log("Error -> " + err.message);
    });    

now I want to find a way to compress the ImageAsset or the ImageSource. Any suggestions? @pap5508 @NickIliev

3reactions
emmanuel128commented, Mar 22, 2017

I used the nativescript-camera and nativescript-imagepicker to take a picture, save it and convert it to base 64. I hope it helps. If anyone has another workaround please let me know.

    // nativescript-camera
    cameraModule.requestPermissions();
    cameraModule.takePicture({
        width: 1280, height: 720, keepAspectRatio: false, saveToGallery: true
    }).then(function (imageAsset) {
        console.log("Result is an image asset instance");
 
        var context = imagepicker.create({
            mode: "single"
        });
    
        context
        .authorize()
        .then(function() {
            return context.present();
        })
        .then(function(selection) {
            console.log("Selection done:");
            selection.forEach(function(selected) {
                selected.getImage().then(res =>{
                    var d = new Date();
                    _photoDateTime = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+
                                     d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
                    viewModel.set("PhotoDateTime", _photoDateTime);

                    viewModel.set("BoardingPassSource", res); 
                    viewModel.set("base64String", res.toBase64String("png"));
                    console.log("image selected");                    
                });
            });
        }).catch(function (e) {
            console.log(e);
        });
        
    }).catch(function (err) {
        console.log("Error -> " + err.message);
    });

Read more comments on GitHub >

github_iconTop Results From Across the Web

ImageSourceModule method toBase64String not working ...
ImageSourceModule method toBase64String not working (both iOS and ... I'm getting this error this.android.compress is not a function .
Read more >
[Resolve]-trying to store image as base64 and using it
When I run this.image = "~/assets/images/account/camera.png" (just a placeholder, even calling a method does not work too) for example it catches an error....
Read more >
Unable to decode base64string to uiimage in iOS swift but ...
I have this code to convert UIImage to base64EncodedString and from base64EncodedString back to UIImage on both iOS and Android app. The problem...
Read more >
Nativescript 6.2 Convert Base64 Image String To Imagesource
ImageSourceModule method toBase64String not working both iOS and Android #3605 to take a picture and then save it to base64 string.
Read more >
ImageSource module - NativeScript Docs
Load image using resource name ... This is similar to loading Bitmap from R.drawable.logo on Android or calling [UIImage imageNamed@"logo"] on iOS. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found