尝试将图像存储为base64并使用它



我在网上找到了一块代码,它似乎可以工作,但不能同时工作。我想这可能是我缺乏理解,但我似乎无法让它按我想要的方式工作。

selectPicture() {               
let context = imagepicker.create({
mode: "single" // use "multiple" for multiple selection
});
var imageBase64
context
.authorize()
.then(function() {
return context.present();
})
.then(function(selection) {
selection.forEach(function(selected) {
imageSourceModule.fromAsset(selected).then((imageSource) => {

imageBase64 = imageSource.toBase64String("jpg",60);
console.log("Image saved successfully!")
console.log(imageBase64)
console.log("test test") //runs fine
this.image = "~/assets/images/account/camera.png" //cant seem to run
console.log("test test 2")
}).catch(function (e) {
// process error
console.log("got error 1")
});
})
}).catch(function (e) {
// process error
console.log("got error 2")
});
},

imageSourceModule.fromAsset(selected).then((imageSource)中,我试图将base64信息保存在另一个变量中,但除了控制台日志字符串之外,似乎什么都做不了。例如,当我运行this.image = "~/assets/images/account/camera.png"(只是一个占位符,即使调用一个方法也不起作用)时,它会捕获一个错误。

可能是什么问题?非常感谢。

更新

我更改了console.log("got error 1")以记录实际更新,得到的是:

undefined不是一个对象(评估"this.image="~/assets/images/account/came.png")*

我现在认为我对外部调用变量的理解有问题。我的变量"image"在的脚本中

data() {
return {
image : ""
}
}

首先检查this变量是什么,因为您没有使用es6箭头函数,所以this可能不是vue实例。第二件事:当异步更改vue变量时,使用$set方法,如:this.$set(this, 'image', '~/assets/images/account/camera.png')

最新更新