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.

Nativescript bind value inside image-picker function

See original GitHub issue

I am using image-picker plugin. I can open images gallery and select single or multiple images.My problem is how to bind the path of the image to the xml image src?! It doesn’t work inside getImage() function.

<Image class="imageSource" src="{{ thumb }}" stretch="none" />

import { Observable } from 'data/observable';
import * as imagepicker from "nativescript-imagepicker";

var counter = 0;
var fs = require('file-system');

export class AssistenceViewModel extends Observable {

    thumb:any;

public addImage(){
    dialogs.action({
        message: "Opções", 
        cancelButtonText: "Cancelar", 
        actions: ["Câmera", "Galeria"]
    }).then(result => {

        console.log("Dialog result: " + result);
        if(result == "Câmera"){

            //Do action1
            console.log("Abrir camera");

        }else if(result == "Galeria"){

            console.log("Abrir galeria");
            let context = imagepicker.create({
                mode: "single"
            });

            context.authorize().then(function() {
                return context.present();
            }).then(function(selection) {

                selection.forEach(function(selected){

                    selected.getImage().then(function(imagesource){

                        var localPath = null;

                        if(platformModule.device.os === "Android"){
                            localPath = selected.android;
                            console.log("localPath android: " +localPath);
                        }else {
                            // selected_item.ios for iOS is PHAsset and not path - so we are creating own path
                            let folder = fs.knownFolders.documents();
                            let path = fs.path.join(folder.path, "Test" + counter + ".png");
                            let saved = imagesource.saveToFile(path, "png");

                            localPath = path;
                            console.log("localPath iOS: " +localPath);
                        }

                        if(localPath){
                            this.thumb = localPath  // this is not working
                            console.log("thumb: "+this.thumb); // this is not working
                        }

                    });

                });                 

            }).catch(function(e) {
                console.log(e);
            });
        }
    });
  }
}

The result for console.log("localPath android: " +localPath);

localPath android: /storage/emulated/0/DCIM/Camera/IMG_20171213_224917038.jpg

but I cannot get any log for this.thumb.

I tried the examples from this repo and from image upload repo but it doesn’t work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
NickIlievcommented, Jan 11, 2018

@madsongr in the case above you are hitting Out of Memory exception due to working with large files that are staying in the memory. Although you are resetting the observable property thumb the memory is not released until the GC is triggered by the JS virtual machine. You can workaround this by loading thumbs or images with smaller sizes (the method getImage() accepts options like maxWidth and maxHeight)

Article about working with image on Android and using the good practices to avoid OOM related errors can be found here

1reaction
NickIlievcommented, Jan 11, 2018

@madsongr there two things that need to be considered:

  • when creating an observable property you should create an observable property with getter/setter with notifyPropertyChange instead of field thumb (or use ‘this.set()’ & ‘this.get()’) e.g.
    get thumb(): any {
        return this._thumb ;
    }
    
    set thumb(value: any ) {
        if (this._thumb !== value) {
            this._thumb = value;
            this.notifyPropertyChange('thumb', value)
        }
    }

  • The second thing you need to consider is the meaning of this when using JavaScript function scope vs TypeScript arrow function

When using




.then(function(selection) { 
    // the meaning of this is different from the global context so use the "cached" context 
    that.thumb = newValue;

the meaning of this is not the global context so you should “cache” the meaning with var that = this; pattern (more here)

When using TypeScript arrow function you can preserve the meaning of this For example

.then((selection) => {
    // this is the same as the global this in your Observable model
    this.thumb = newValue;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Nativescript bind value inside image-picker function
I am using image-picker plugin. I can open images gallery and select single or multiple images. My problem is how to bind the...
Read more >
Image Picker - NativeScript Docs
Create imagepicker in single or multiple mode to specifiy if the imagepicker will be used for single or multiple selection of images. TypeScript....
Read more >
@nativescript/core | Yarn - Package Manager
NativeScript empowers you to access native APIs from JavaScript directly. Currently iOS and Android runtimes are provided for rich mobile development across ...
Read more >
Adding a functional profile page to your Nativescript Vue app
In a previous post I described how to create a Nativescript Vue v2 app with a login screen and Firebase for authentication.
Read more >
Complete Guide to NativeScript Plugin - eduCBA
Proxy lab as a plugin is similar to a getter/setter function that enables developers to get, set or clear any proxy related settings...
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