[英]Ionic(Capacitor) - How to view image after capturing it?
I am able to capture an image with ionic capacitor and convert it to a base 64 string so I can send it to the API.我能够使用离子电容器捕获图像并将其转换为 base 64 字符串,以便将其发送到 API。 But how do I show the image on the HTML side after capturing it so the user can see it before it is uploaded?
但是如何在捕获图像后在 HTML 端显示图像,以便用户在上传之前看到它?
Below is the code when I click on the submit button以下是我单击提交按钮时的代码
CreateMainStore(MainStoreForm: FormGroup) {
const mainStore: MainStoreModel = MainStoreForm.value;
let formData = new FormData();
// formData.append("mainStoreId", mainStore.mainStoreId.toString());
formData.append("mainStoreName", mainStore.mainStoreName);
formData.append("imageTitle", mainStore.imageTitle);
formData.append("mainStoreLogo", this.Logo);
var object = {};
formData.forEach((value, key) => {
object[key] = value;
});
var json = JSON.stringify(object);
this.dataService.AddMainStore(mainStore);
this.router.navigate([""]);
} // Create MainStore
This is the code I use to capture the image and store it in a Logo variable这是我用来捕获图像并将其存储在 Logo 变量中的代码
async selectImageSource() {
const buttons = [
{
text: "Take Photo",
icon: "camera",
handler: () => {
this.addImage(CameraSource.Camera);
},
},
{
text: "Gallery",
icon: "image",
handler: () => {
this.addImage(CameraSource.Photos);
},
},
];
const actionSheet = await this.actionSheetCtrl.create({
header: "Select Image Source",
buttons,
});
await actionSheet.present();
} // End of select image source
async addImage(source: CameraSource) {
const image = await Camera.getPhoto({
quality: 60,
allowEditing: true,
resultType: CameraResultType.Base64,
height: 128,
direction: CameraDirection.Rear,
source,
});
this.Logo = image.base64String;
}
In your HTML file just try在您的 HTML 文件中尝试
<img [src]="Logo"/>
it will show the image它将显示图像
First I needed to import:首先我需要导入:
import { DomSanitizer } from "@angular/platform-browser";
Next I prepended "data:image;base64,"
to the base64 string that I received from the Camera plugin.接下来,我将
"data:image;base64,"
添加到我从相机插件收到的 base64 字符串前面。
Then I needed to sanitize this.logo
by making use of the following method:然后我需要使用以下方法对
this.logo
进行清理:
transformer() {
return this.sanitizer.bypassSecurityTrustResourceUrl(this.Logo);
}
lastly I made use of the above answer from "Edmad Abodou" to call the method using the img tag:最后,我利用“Edmad Abodou”的上述答案使用 img 标签调用该方法:
<img [src]="transformer()" />
``
And this allowed me to view the image after the user uploaded it.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.