类型“ ImageAsset”上不存在属性“ toBase64String”

时间:2019-08-17 05:41:48

标签: nativescript nativescript-angular

我正在尝试使用原始脚本和角度将捕获的摄像机图像保存到Couchbase。

遵循https://dzone.com/articles/save-captured-images-in-a-nativescript-angular-app上Nic Raboy的教程,但收到以下错误:

public capture() {
           Camera.takePicture({ width: 300, height: 300, keepAspectRatio: true, saveToGallery: false }).then(picture => {
            let base64 = picture.toBase64String("png", 70);
            this.database.createDocument({
                "type": "image",
                "image": base64,
                "timestamp": (new Date()).getTime()
            });
            this.images.push(picture);
        }, error => {
            console.dump(error);
        });
    }

错误TS2339:类型'ImageAsset'上不存在属性'toBase64String'。

完整源代码

import { Component, OnInit } from "@angular/core";
import { Couchbase } from "nativescript-couchbase";
import * as Camera from "camera";
import * as ImageSource from "image-source";
@Component({
    selector: "ns-app",
    templateUrl: "app.component.html",
})
export class AppComponent implements OnInit {
    public database: any;
    public images: Array<any>;
    public constructor() {
        this.database = new Couchbase("image-database");
        this.database.createView("images", "1", function(document, emitter) {
            if(document.type && document.type == "image") {
                emitter.emit(document._id, document);
            }
        });
        this.images = [];
    }
    public ngOnInit() {
        let rows = this.database.executeQuery("images");
        for(let i = 0; i < rows.length; i++) {
            this.images.push(ImageSource.fromBase64(rows[i].image));
        }
    }
    public capture() {
        Camera.takePicture({ width: 300, height: 300, keepAspectRatio: true, saveToGallery: false }).then(picture => {
            let base64 = picture.toBase64String("png", 70);
            this.database.createDocument({
                "type": "image",
                "image": base64,
                "timestamp": (new Date()).getTime()
            });
            this.images.push(picture);
        }, error => {
            console.dump(error);
        });
    }
}

1 个答案:

答案 0 :(得分:0)

pictureImageAsset类型,您必须将其转换为ImageSource才能使用toBase64String(...)

...

ImageSource.fromAsset(picture)
  .then(source => {
      let base64 = source.toBase64String("png", 70);
      ...
  })
  .catch(err => {
    console.log(err);
  });