如下所示,我使用的是[src]属性.我要做的是预览从设备的相机拍摄的图像.请参阅下面的其他打字稿代码.
<img [src]="lastImage" style="width: 100%" [hidden]="lastImage === null"> <button ion-button icon-left (click)="presentActionSheet()"> <ion-icon name="camera"></ion-icon>Select Image </button>
这是.ts代码
lastImage: string = null; public presentActionSheet() { let actionSheet = this.actionSheetCtrl.create({ title: 'Select Image Source',buttons: [ { text: 'Load from Library',handler: () => { this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY); } },{ text: 'Use Camera',handler: () => { this.takePicture(this.camera.PictureSourceType.CAMERA); } },{ text: 'Cancel',role: 'cancel' } ] }); actionSheet.present(); } public takePicture(sourceType) { // Create options for the Camera Dialog var options = { quality: 100,sourceType: sourceType,saveToPhotoAlbum: false,correctOrientation: true }; // Get the data of an image this.camera.getPicture(options).then((imagePath) => { // Special handling for Android library if (this.platform.is('ios') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) { alert('IF'); this.filePath.resolveNativePath(imagePath).then(filePath => { let correctPath = filePath.substr(0,filePath.lastIndexOf('/') + 1); let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1,imagePath.lastIndexOf('?')); // alert(correctPath); alert(correctPath + currentName); this.lastImage = correctPath + currentName; // this.copyFileToLocalDir(correctPath,currentName,this.createFileName()); }); } else { alert('ELSE'); // This part runs var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1); var correctPath = imagePath.substr(0,imagePath.lastIndexOf('/') + 1); alert(cordova.file.dataDirectory + currentName); // This returns proper image path this.lastImage = cordova.file.dataDirectory + currentName; alert(this.lastImage); // this also has the image path. this.copyFileToLocalDir(correctPath,this.createFileName()); } },(err) => { this.presentToast('Error while selecting image.'); }); }
现在,当我选择图像使用相机然后它打开相机,我拍了一张照片.但不知何故,我上面的HTML中没有预览照片,我正在使用[src] =“lastImage”.我的代码有什么问题,它没有显示来自相机的任何图像?
UPDATE
我也使用了normalizeURL,我发现here如下!
import { normalizeURL } from 'ionic-angular'; this.lastImage = normalizeURL(cordova.file.dataDirectory + currentName);
这段代码会发生什么,它用http:// localhost:8080替换file:///部分,而我从摄像机拍摄的照片本地而不是任何服务器,并希望在img标签上显示.
解决方法
他,我建议使用base64将图像设置为img标签,检查下一个代码:
控制器属性
private base64Image: any = false;
在你的控制器构造函数集中:“public domSanitizer:DomSanitizer”作为参数,这允许对角度表示图像是“安全的”.
控制器方法
takePicture() { const options: CameraOptions = { quality: 10,destinationType: this.camera.DestinationType.DATA_URL,encodingType: this.camera.EncodingType.JPEG,mediaType: this.camera.MediaType.PICTURE } this.camera.getPicture(options).then((imageData) => { // imageData is either a base64 encoded string or a file URI // If it's base64: this.base64Image = 'data:image/jpeg;base64,' + imageData; },(err) => { this.message("Error,your camera device not work"); });
}
在您的视图文件中
<img *ngIf="base64Image != 'false'" [src]="domSanitizer.bypassSecurityTrustUrl(base64Image)">