如何获得新的图像路径

时间:2020-01-22 14:06:05

标签: typescript nativescript

我需要将保存的图像路径传递给新组件,但无法从if条件中获取它。这是我的代码:

 public imagePath: string;
    constructor() {}
    async selectPhoto(): Promise<any> {
        let context = imagepicker.create({
            mode: "single"
        });
        await context.authorize();
        const selection  = await context.present();
        const source = new ImageSource();
        source.fromAsset(selection[0])
            .then((imageSource: ImageSource) => {
                const folderPath: string = knownFolders.documents().path;
                const fileName = 'img_' + new Date().getTime()+ '.jpg';
                const filePath = path.join(folderPath, fileName);
                console.log('Path: ',filePath);
                const saved: boolean = imageSource.saveToFile(filePath, "jpg");
                if (saved) {
                    console.log("Image saved");
                    this.imagePath = filePath;
                }
            })
            .catch((e) => {
                console.log("Error ");
                console.log(e);
            });
            console.log("New path: ", this.imagePath);
            return selection[0];
    } 

我尝试将图像保存为不同的函数并获得filePath,但是它不起作用。 console.log("New path: ", this.imagePath)返回错误:新路径:未定义

0 个答案:

没有答案