Comments (5)
Use the getImage
instead decodeUri
on the selected
: https://github.com/NativeScript/nativescript-imagepicker/blob/release/source/imagepicker.d.ts#L45
Also if you are going to use functions for the then and catch callbacks of the promises, you will probably have to add a:
var that = this;
before the var context = ...
and then at the end that.pic_list.push(...)
from nativescript-imagepicker.
Not sure if this issue should be closed, im having the same problem and im using arrow functions, i can get the value but only inside the function, even if i console log the same variable, inside the function will have the value, but outside it's undefined.
code:
maint.ts:
export function onSelectSingleTap(args) {
let context = imagepicker.create({ mode: "single" });
startSelection(context, true);
}
function startSelection(context, isSingle) {
context
.authorize()
.then(function() {
list.items = [];
return context.present();
})
.then(function(selection) {
console.log("Selection done:");
selection.forEach(function(selected) {
console.log("----------------");
console.log("uri: " + selected.uri);
if (isSingle) {
selected.getImage({ maxWidth: 200, maxHeight: 200, aspectRatio: 'fill' })
.then((imageSource) => {
imageSrc.src = imageSource;
model.base64Image = imageSource.toBase64String('png');
console.log('model.base64Image: ', model.base64Image);
});
} else {
imageSrc.visibility = 'hidden';
}
});
list.items = selection;
}).catch(function (e) {
console.log(e);
});
}
export function chek64() {
console.log('model.base64Image: ', model.base64Image);
}
app.model.ts:
export class AppModel {
private _base64Image: string;
private _imageSource: string;
constructor() {}
public get base64Image(): string{
return this._base64Image;
}
public set base64Image(value: string) {
this._base64Image = value;
}
public get imageSource(): string{
return this._imageSource;
}
public set imageSource(value: string) {
this._imageSource = value;
}
}
after upload the image i can see the base 64 code in the console, but just after that i call the function check64
and i get undefined
on the same variable!
from nativescript-imagepicker.
Weird thing! if i instance the model in the same method it work's
code:
function startSelection(context, isSingle) {
model = new AppModel();
context
.authorize()
.then(function() {
list.items = [];
return context.present();
})
.then(function(selection) {
console.log("Selection done:");
selection.forEach(function(selected) {
console.log("----------------");
console.log("uri: " + selected.uri);
if (isSingle) {
selected.getImage({ maxWidth: 200, maxHeight: 200, aspectRatio: 'fill' })
.then((imageSource) => {
imageSrc.src = imageSource;
model.base64Image = imageSource.toBase64String('png');
console.log('model.base64Image: ', model.base64Image);
});
} else {
imageSrc.visibility = 'hidden';
}
});
list.items = selection;
}).catch(function (e) {
console.log(e);
});
}
export function chek64() {
console.log('model.base64Image: ', model.base64Image);
}
before this i was creating the instance when the page loaded
from nativescript-imagepicker.
Note*: The code above just fix the issue if you don't have any problem creating a new instance each time a user upload a photo, it didn't work for me since i can't create a different instance of the model each time a user upload a photo, so i'm still having the issue, im thinking that's maybe something with the context.
export function onSelectSingleTap(args) {
let context = imagepicker.create({ mode: "single" });
startSelection(context, true);
}
export function startSelection(context, isSingle) {
context
.authorize()
.then(() => {
list.items = [];
return context.present();
})
.then((selection) => {
selection.forEach((selected) => {
if (isSingle) {
selected.getImage({ maxWidth: 200, maxHeight: 200, aspectRatio: 'fill' })
.then((imageSource) => {
imageSrc.src = imageSource;
base64code = imageSource.toBase64String('png');
set64();
});
} else {
imageSrc.visibility = 'hidden';
}
});
list.items = selection;
}).catch( (e) => {
console.log(e);
});
}
export function set64() {
console.log('base64code: ', base64code);
model.base64Image = base64code;
console.log('model.base64Image: ', model.base64Image); // base64 string correctly printed
console.log('model.imageSource: ', model.imageSource); // This print a static value 'TEST'
}
export function check64() {
console.log('+++++++++++++++++++++++++++++++++++++++++++');
console.log('model.imageSource: ', model.imageSource); // This print the same static value 'TEST'
console.log('model.base64Image', model.base64Image); // undefined
}
when i upload a photo i get the console.logs form the set64
function and they show me the correct information, but then with a button i call the check64
function and even though i have the same value when access other property, model.base64Image
prints undefined.
from nativescript-imagepicker.
FYI, i was able to fix this by creating a variable inside the same file where i call the function to take a picture, but into a 'global' context
When the page loaded it triggers a function, and i was creating the model inside that function:
// Created this variable inside the global context and use it to store the information inside the imagepicker callback
let imageBase64String: string;
export function pageLoaded(args: EventData) {
let page = <Page>args.object;
model = new AppModel()
list = page.getViewById("urls-list");
imageSrc = page.getViewById("imageSrc");
}
...
So just before save my model i assign the value of imageBase64String
to the correct property inside the model, and it works.
not sure if that's the right solution, but i couldn't find anything else to make it work.
from nativescript-imagepicker.
Related Issues (20)
- No reliable way to determine selected media's type HOT 5
- Error on multiple number of picking Calling js method onStart failed
- I can only select files from the Screenshots and Camera directories
- iOS src is empty HOT 9
- Imagepicker do not take 100% height HOT 1
- [android]error not easy to "filter" HOT 1
- Can ImagePicker be used to select videos from camera roll? If not, any suggestions? HOT 2
- Feature request: Better errors
- [IOS]multiple dependencies with different sources for `QBImagePickerController` issue when install with @nstudio/nativescript-camera-plus HOT 1
- How can i select directly from Gallery rather than downloads
- Plugin translation
- [android] No path for returned image(s) HOT 1
- JS: TypeError: Cannot read property 'startActivityForResult' of undefined
- Class constructor Observable cannot be invoked without 'new' Nativescript 7.0.0 HOT 3
- IOS on dismiss picker canceled error is not fired
- Plug-in does not work in NativeScript 8 HOT 2
- Android: How to get file extension of images selected using @nativescript/imagepicker HOT 1
- What is the migration guide from NS-6 to NS-8 for nativescript-imagepicker?
- "fromAsset() is deprecated. Use ImageSource.fromAsset()" while using ImageSource.fromAsset()
- [Image Picker] Images in iPhone Library not visible
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nativescript-imagepicker.