angular - Ionic - 拍照后再次调用相机?
问题描述
我目前正在开发一个需要至少 4 张照片的应用程序。每次拍照时我都会尝试重新启动相机,这样他们就不必再次单击应用菜单上的拍照按钮。到目前为止,这是我的代码:
startCamera() {
if(this.pictureList.length >= 4) {
this.alertController.create({
title: this.singleton.TITLE_ERROR,
message: this.singleton.MESSAGE_TOOMANYPHOTOS,
buttons: [
{
text: 'Ok',
}
]
}).present();
return;
}
let imageData = "";
this.camera.getPicture(this.options).then((imageData) => {
this.pictureList.push(imageData);
let temp = [];
for (let i of this.pictureList) i && temp.push(i);
this.pictureList = temp;
this.slides.refresh();
}, (err) => {
console.log(err);
});
}
但是,摄像头不重启就进摄像头了?有什么理由不这样做吗?我认为这与我试图在承诺中调用函数有关吗?
谢谢
解决方案
也许通过递归你可以做到,我给你留下一个未经证实的例子,但这在逻辑上可以为你服务:
我基于将其保存在 Base 64 中的应用程序进行了示例,但根据需要将推送应用到列表中。
pictureList: any[];
ngOnInit(): void {
this.pictureList = null;
this.optionsCamera = {
quality: 50,
destinationType: this._camera.DestinationType.DATA_URL,
encodingType: this._camera.EncodingType.JPEG,
mediaType: this._camera.MediaType.PICTURE,
saveToPhotoAlbum: true
};
this.optionsGallery = {
quality: 50,
destinationType: this._camera.DestinationType.DATA_URL,
sourceType: this._camera.PictureSourceType.PHOTOLIBRARY
};
}
startCamera(): void {
if (this.pictureList.length <= 4) {
console.log('Open in length: ' + this.pictureList.length);
this.getPhoto(this.optionsCamera);
this.startCamera();
}
}
getPhoto(options): void {
this._camera.getPicture(options).then(
imageData => {
let base64Image = "data:image/jpeg;base64," + imageData;
this.pictureList.push(base64Image);
},
err => {
console.log("Could not open the camera: " + err);
}
);
}
推荐阅读
- excel - .Body 不会编译,但引号和换行括号似乎是正确的
- objective-c - NSMutableDictionary 和 mutableCopy
- apache-spark - Hadoop 3 和 spark.sql:同时使用 HiveWarehouseSession 和 spark.sql
- r - r 序列问题 - 给定序列中的最大更改数
- c++ - 根据运行时提供的类型执行模板函数
- qbasic - 使用 repl.it 在 QuickBasic 中编程?
- excel - 如何在 Excel VBA 中发送和返回范围/数组
- jenkins - Jenkins Scripted Pipeline 将工件保留 90 天,或 10 次构建价值,以更大的为准
- watson-assistant - 可以在 Watson Assistant 对话框的槽的“找到”选项中检查哪些条件?
- angular - NgOnInit 生命周期钩子中的 Angular 8 材质表 @ViewChild 未定义,即使使用静态 true 选项