gpt4 book ai didi

javascript - 照片未显示 Ionic 3

转载 作者:行者123 更新时间:2023-12-03 01:04:33 27 4
gpt4 key购买 nike

我正在构建一个 Ionic 应用程序,并尝试使用相机 native 插件来拍照,然后将其显示在屏幕上,当我创建一个全新的项目并将其放入其中时,脚本正在运行,但它不适用于我正在从事的项目。

我以为我的 package.json 有问题,但我更新了所有软件包,删除了文件和相机插件,再次安装了它们,但问题仍然相同,这是我得到的输出 enter image description here

但是当我创建一个新项目并尝试该脚本时,照片显示得很完美!

我只是陷入困境,不知道是什么导致了这种冲突,也不知道去哪里寻找,请帮忙

HTML:

<ion-card *ngIf=resultImageArray>
<img src="{{ resultImageArray }}" />
</ion-card>

TS:

import { Component } from '@angular/core'; 
import { NavController, ToastController } from 'ionic-angular';
import {Camera, CameraOptions} from "@ionic-native/camera";
import {File} from '@ionic-native/file';
export class HomePage {

public imageURI: any;
public imageName: any;
public fileCreated: boolean = false;
public imageString: any;
resultImageArray: any;
constructor(public navCtrl: NavController, private file: File, private
camera: Camera, private toastCtrl: ToastController,) {}
getImageFromCamera() {
const options: CameraOptions = {
quality: 20,
saveToPhotoAlbum: true,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.CAMERA,
encodingType: this.camera.EncodingType.JPEG,
allowEdit: true
};

this.camera.getPicture(options).then((imageData) => {
this.imageURI = imageData;
this.imageName = imageData.substr(imageData.lastIndexOf('/') + 1);
this.file.checkDir(this.file.externalRootDirectory, 'ImagesDemo')
.then(() => {
this.fileCreated = true;
}, (err) => {
console.log("checkDir: Error");
console.log(JSON.stringify(err));
this.presentToast("checkDir Failed");
});
if (this.fileCreated) {
this.presentToast("Directory Already exist");
}
else {

this.file.createDir(this.file.externalRootDirectory,"ImagesDemo",true)
.then((res) => {
this.presentToast("Directory Created");
}, (err) => {
console.log("Directory Creation Error:");
console.log(JSON.stringify(err));
});
}

let tempPath = this.imageURI.substr(0,this.imageURI.lastIndexOf('/')+1);
let androidPath = this.file.externalRootDirectory + '/ImagesDemo/';
this.imageString = androidPath + this.imageName;

this.file.moveFile(tempPath, this.imageName, androidPath,this.imageName)
.then((res) => {
this.presentToast("Image Saved Successfully");
this.readImage(this.imageString);

}, (err) => {
console.log("Image Copy Failed");
console.log(JSON.stringify(err));
this.presentToast("Image Copy Failed");
});

this.toDataURL(this.imageURI, function (dataUrl) {
console.log('RESULT:' + dataUrl);
});
}, (err) => {
console.log(JSON.stringify(err));
this.presentToast(JSON.stringify(err));
});
}


presentToast(msg) {
let toast = this.toastCtrl.create({
message: msg,
duration: 2000
});
toast.present();
}

toDataURL(url, callback) {
let xhr = new XMLHttpRequest();
xhr.onload = function () {
let reader = new FileReader();
reader.onloadend = function () {
callback(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}

readImage(filePath) {
let tempPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let imageName = filePath.substr(filePath.lastIndexOf('/') + 1);

this.file.readAsDataURL(tempPath, imageName)
.then((res) => {
this.presentToast("Image Get Done");
this.resultImageArray = res;
}, (err) => {
this.presentToast("Image Get Error");
});
}
}

这是我远程调试应用程序时从控制台收到的错误:

(index):1 拒绝加载图像“data:image/jpeg;base64,/9j/4QBAAl+B//2Q==”,因为它违反了以下内容安全策略指令:“default-src *” 。请注意,“img-src”未明确设置,因此“default-src”用作后备。

我剪切了基于 64 的图像代码,因为它很长,阅读起来可能不太舒服,那么为什么当我创建新项目时它显示没有错误,而在这个特定项目中它不起作用?

最佳答案

好吧,我想出了这个,所以我想有人可能正在寻找这个

我将此行添加到我的index.html 文件中

<meta http-equiv="Content-Security-Policy" content="default-src *; 
style-src 'self' 'unsafe-inline';
script-src 'self' 'unsafe-inline' 'unsafe-eval';
img-src 'self' data: https://s-media-cache-ak0.pinimg.com;
script-src 'self' https://maps.googleapis.com;
" />

现在一切正常

关于javascript - 照片未显示 Ionic 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52467586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com