Nativescript imagepicker .getImage()不是函数错误

时间:2018-06-01 18:50:28

标签: nativescript

我一直在尝试实现这个question的答案,但一直得到错误“selected.getImage不是函数”。

此时我尝试了多个不同的代码示例,我很难过。看起来这是一个类型错误,但我不确定在哪里可以纠正这个。

我希望选择单个图像并返回该图像的路径以便上传到服务器。我不需要在设备上显示它,尽管我认为这是一个选项。似乎很容易,但显然我错过了一些东西。

我正在使用v.6.0.1或imagepicker插件。我引用了代码,但此时我正在使用Shiva Prasad在上述问题中提供的确切示例。

按Max Vollmer添加代码:

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);
    });

1 个答案:

答案 0 :(得分:0)

昨天我遇到了完全相同的错误。

我直接在“选择的”上使用fromAsset函数,因为显然在此插件的新版本中,“选择的”是资产。这样就得到了一个imageSource,可以使用“ saveToFile”功能将资产复制到新位置(使用TNS的fileSystemModule获取该位置)。使用此新位置的路径作为您的UI,将显示图像。您也可以从我用于上传的位置fileSystemModule.File.fromPath(path);创建文件对象。

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

                    let file;

                    if (selected._android) {

                        file = fileSystemModule.File.fromPath(selected._android);
                        //viewModel.uploadFile(file);

                    }else{

                        imageSourceModule.fromAsset(selected).then((imageSource) => {

                        const folder = fileSystemModule.knownFolders.documents().path;
                        const fileName = "Photo.png"; 
                        const path = fileSystemModule.path.join(folder, fileName);
                        const saved = imageSource.saveToFile(path, "png");

                        if (saved) {
                            console.log("Image saved successfully!");
                            file = fileSystemModule.File.fromPath(path);
                            //viewModel.uploadFile(file);
                        }else{
                            console.log("Error! - image couldnt save.");
                        }
                    });
                }
            });
            }).catch(function (e) {
                console.log(e);
                // process error
            });

说明

未注释的代码段(//viewModel.uploadFile(file);),viewModel引用(在您的应用程序中将有所不同)和功能:例如uploadFile是传递文件以上传文件或将其设置为image.src

确保在顶部声明imageSourceModule

const imageSourceModule = require("tns-core-modules/image-source");
相关问题