Nativescript bind value inside image-picker function
See original GitHub issueI 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:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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
thumbthe 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 likemaxWidthandmaxHeight)Article about working with image on Android and using the good practices to avoid OOM related errors can be found here
@madsongr there two things that need to be considered:
notifyPropertyChangeinstead of fieldthumb(or use ‘this.set()’ & ‘this.get()’) e.g.thiswhen using JavaScript function scope vs TypeScript arrow functionWhen using
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
thisFor example