【问题标题】:trying to store image as base64 and using it尝试将图像存储为 base64 并使用它
【发布时间】:2021-04-25 11:51:53
【问题描述】:

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

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/camera.png"')*

我现在认为我对外部调用变量的理解存在问题。我的变量“图像”在脚本中

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

【问题讨论】:

标签: ios image vue.js base64 nativescript


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    • 2019-06-01
    • 2017-07-11
    • 2017-01-11
    • 1970-01-01
    • 2019-12-18
    相关资源
    最近更新 更多