0

我正面临这个 Promise 问题,当我使用 ImagePicker 插件从 Gallery 中选择一张图片时,我想返回一个 Promise,然后使用该文件。

这是我的代码:

takePictureFromGallery() : any {
        let context = imagepicker.create({
            mode: "single"
        });
        context
            .authorize()
            .then(function() {
                return context.present();
            })
            .then(function(selection) {
                selection.forEach(function(selected) {
                    dialogs.prompt({
                        title: Globals.APP_NAME,
                        message: "Picture description (optional)",
                        okButtonText: "OK",
                        cancelButtonText: "Cancel",
                        inputType: dialogs.inputType.text
                    }).then(r => {
                        if (r.result) {
                            parameters.Headers['Description'] = "";
                            if (r.text != '') parameters.Headers['Description'] = r.text;
                        }
                        let imageSource = new imageSourceModule.ImageSource();
                        imageSource.fromAsset(selected).then((source) => {
                            let savepath = fileSystem.knownFolders.documents().path;
                            let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                            let filepath = fileSystem.path.join(savepath, filename);

                            var picSaved = source.saveToFile(filepath, "jpg");

                            return new Promise((resolve, reject) => {
                                if (picSaved) resolve(filepath);
                                else reject();
                            });

                        })
                    })
                })
            }).catch(function (e) {
                this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
            });

    }

该方法属于一个名为 PictureUploader 的类。在这个类之外我有这个代码:

let pu = new PictureUploader();
pu.takePictureFromGallery()
    .then(s => {
        console.log(s);
})

undefined is not an object当我尝试运行该.then()方法时,我得到了。

有什么帮助吗?谢谢!

4

2 回答 2

2

您需要将您的promise声明放在最开始,如下所示:

takePictureFromGallery() : any {
    return new Promise((resolve, reject) => {
        let context = imagepicker.create({
            mode: "single"
        });
        context
            .authorize()
            .then(function() {
                return context.present();
            })
            .then(function(selection) {
                selection.forEach(function(selected) {
                    dialogs.prompt({
                        title: Globals.APP_NAME,
                        message: "Picture description (optional)",
                        okButtonText: "OK",
                        cancelButtonText: "Cancel",
                        inputType: dialogs.inputType.text
                    }).then(r => {
                        if (r.result) {
                            parameters.Headers['Description'] = "";
                            if (r.text != '') parameters.Headers['Description'] = r.text;
                        }
                        let imageSource = new imageSourceModule.ImageSource();
                        imageSource.fromAsset(selected).then((source) => {
                            let savepath = fileSystem.knownFolders.documents().path;
                            let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                            let filepath = fileSystem.path.join(savepath, filename);

                            var picSaved = source.saveToFile(filepath, "jpg");

                            if (picSaved) resolve(filepath);
                            else reject();

                        })
                    })
                })
            }).catch(function (e) {
                this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
            });

    });
}

并且您还需要注意在catch块中拒绝承诺(示例中未显示)

于 2018-06-04T15:28:27.883 回答
1

我建议你返回一个 observable 。

你可以在这里阅读 Promise vs Observable

以同样的方式实现为什么就像这样

takePictureFromGallery() : observable<any> {
return new observable((o) => {
    let context = imagepicker.create({
        mode: "single"
    });
    context
        .authorize()
        .then(function() {
            return context.present();
        })
        .then(function(selection) {
            selection.forEach(function(selected) {
                dialogs.prompt({
                    title: Globals.APP_NAME,
                    message: "Picture description (optional)",
                    okButtonText: "OK",
                    cancelButtonText: "Cancel",
                    inputType: dialogs.inputType.text
                }).then(r => {
                    if (r.result) {
                        parameters.Headers['Description'] = "";
                        if (r.text != '') parameters.Headers['Description'] = r.text;
                    }
                    let imageSource = new imageSourceModule.ImageSource();
                    imageSource.fromAsset(selected).then((source) => {
                        let savepath = fileSystem.knownFolders.documents().path;
                        let filename = 'img_' + new Date().getTime() + '.' + "jpg";
                        let filepath = fileSystem.path.join(savepath, filename);

                        var picSaved = source.saveToFile(filepath, "jpg");

                        if (picSaved){
                           o.next(filepath);
                           o.complete();
                          }   
                        else o.error();

                    })
                })
            })
        }).catch(function (e) {
            this._feedback.error({message: "You must allow " + Globals.APP_NAME + " access to your Gallery"});
        });

});
}
于 2018-06-11T21:32:48.133 回答