我正在尝试保存捕获的相机图像到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);
});
}
}
发布于 2019-08-17 20:59:46
picture
是ImageAsset类型,您必须将其转换为ImageSource才能使用toBase64String(...)
。
...
ImageSource.fromAsset(picture)
.then(source => {
let base64 = source.toBase64String("png", 70);
...
})
.catch(err => {
console.log(err);
});
https://stackoverflow.com/questions/57533589
复制相似问题