首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型“ImageAsset”上不存在属性“”toBase64String“”

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

Stack Overflow用户
提问于 2019-08-17 13:41:49
回答 1查看 228关注 0票数 0

我正在尝试保存捕获的相机图像到Couchbase与本机脚本和角度。

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

代码语言:javascript
运行
复制
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’。

完整源代码

代码语言:javascript
运行
复制
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);
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-08-17 20:59:46

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

代码语言:javascript
运行
复制
...

ImageSource.fromAsset(picture)
  .then(source => {
      let base64 = source.toBase64String("png", 70);
      ...
  })
  .catch(err => {
    console.log(err);
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57533589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档