Nativescript imagepicker .getImage()不是函数错误 [英] Nativescript imagepicker .getImage() is not a function error

查看:118
本文介绍了Nativescript imagepicker .getImage()不是函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试对此

I have been trying to implement the answer to this question but keep getting the error "selected.getImage is not a function".

这时我已经尝试了多个不同的代码示例,但我感到很困惑.看来这是一个类型错误,但是我不确定在哪里可以纠正此错误.

I have tried multiple different code examples at this point and I'm stumped. It seems like this is a type error, but I'm not sure where I can correct this.

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

I am looking to select a single image and return the path to that image in order to upload to the server. I don't need to display it on the device, though that is an option I suppose. Seems easy enough, but apparently I'm missing something.

我正在使用6.0.1版或imagepicker插件.我引用了该代码,但在这一点上,我使用的是上述问题中Shiva Prasad提供的确切示例.

I'm using v. 6.0.1 or the imagepicker plugin. I'd quote the code, but at this point I am using the exact example provided by Shiva Prasad in the above question.

为每个Max Vollmer添加代码:

Adding code per 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);
    });

推荐答案

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

I was facing the exact same error yesterday.

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

I use the fromAsset function directly on the "selected" because apparently with the new version of this plugin, "selected" is an Asset. So you got an imageSource and you can use the "saveToFile" function that will copy the Asset into a new location (get this location using fileSystemModule from TNS). Use the path of this new location for your UI, and the image will appear. You can also create a file object from this location fileSystemModule.File.fromPath(path);, I use for upload.

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是您传递文件以将其上传或设置为

The uncommented snippet (//viewModel.uploadFile(file);), viewModel reference (will be different on your app) and the function: uploadFile for example is where you would pass the file to upload it or set it to the image.src etc

确保在顶部声明imageSourceModule.

const imageSourceModule = require("tns-core-modules/image-source");

这篇关于Nativescript imagepicker .getImage()不是函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆