如何从本地存储中获取图像,然后在Native Script中将其显示在屏幕上。我正在使用nativescript-imagepicker和fs

时间:2017-10-18 19:23:29

标签: javascript nativescript

我是Native Script的新手。如何从本地存储中显示图像?我使用了nativescript-imagepicker,但后来我不知道图像的存储位置。我正在使用此代码。

var imagepicker = require("nativescript-imagepicker");

var context = imagepicker.create({ mode: "single" }); // use "multiple" for multiple selection

let SlcImg = ""; 

exports.imgPic = function() {
    context
    .authorize()
    .then(function() {
        return context.present();
    })
    .then(function(selection) {
        selection.forEach(function(selected) {
                SlcImg = path;
        })
    }).catch(function (e) {
        console.log(e);
    });
}

我正在尝试设置SlcImg的路径。在我的应用程序的xml部分中,这是代码。

<Image src="{{imageSource}}" class="imageCss" stretch="aspectFill"></Image>

1 个答案:

答案 0 :(得分:0)

var context = imagepickerModule.create({
    mode: "single" // allow choosing single image
});
context
    .authorize()
    .then(function () {
        return context.present();
    })
    .then(function (selection) {
        console.log("Selection done:");
        setTimeout(() => {
            selection.forEach(function (selected) {
                selected.getImage().then((source) => {
                    console.log(selected.fileUri); // this is the uri you need
                });     
            });
        }, 1000);            
    }).catch(function (e) {
        console.log(e);
    });

使用fileUri。而且setTimeout是可选的,在没有它的项目中,应用程序屏幕空白一秒钟。