我正面临这个 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()
方法时,我得到了。
有什么帮助吗?谢谢!