离子3照片库图像未显示在img标签上 [英] ionic 3 photo library image not displayed on img tag
问题描述
我按照离子文档上的指示此处和Github doc 这里。到目前为止,我可以检索所有库项目信息。但我从来没有成功地在img标签上显示照片。
I followed the instruction on ionic doc here and Github doc here. Up to now I can retrieve all library item info. But I never succeed in displaying the photos on img tab.
这是我的 home.ts :
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { PhotoLibrary } from '@ionic-native/photo-library';
import { Platform } from 'ionic-angular';
import { ChangeDetectorRef } from '@angular/core';
import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
library:any;
constructor(public navCtrl: NavController, private photoLibrary: PhotoLibrary, private platform: Platform,
private cd: ChangeDetectorRef, private sanitizer: DomSanitizer) {
this.library = [];
this.fetchPhotos();
}
fetchPhotos() {
this.platform.ready().then(() => {
this.library = [];
this.photoLibrary.requestAuthorization().then(() => {
this.photoLibrary.getLibrary({
thumbnailWidth: 512,
thumbnailHeight: 384,
itemsInChunk: 100,
chunkTimeSec: 0.5,
useOriginalFileNames: false
}).subscribe({
next: (chunk) => {
this.library = this.library.concat(chunk);
//this.library = this.library.slice(0, 9); // To take top 10 images
this.cd.detectChanges();
},
error: (err: string) => {
if (err.startsWith('Permission')) {
console.log('permissions weren\'t granted')
} else { // Real error
console.log('getLibrary error: ');
console.log(err);
}
},
complete: () => {
// Library completely loaded
console.log('done getting photos');
}
});
});
});
}
imgUrl(imgUrl) {
let url: SafeUrl = this.sanitizer.bypassSecurityTrustUrl(imgUrl);
return url;
}
}
这是主页。 html :
<ion-content padding>
<img *ngFor="let libraryItem of library" [src]="imgUrl(libraryItem.thumbnailURL)" />
</ion-content>
我总是得到空的img框 src = null
。
如果我不使用 DomSantinizer ,它会显示所有照片的警告:
I always got empty img box with src=null
.
If I don't use DomSantinizer, it will show the warning for all photos:
警告:清理不安全的URL值
cdvphotolibrary:// thumbnail?photoId = xxx& width = xx& height = xx& quality = 0.5
WARNING: sanitizing unsafe URL value cdvphotolibrary://thumbnail?photoId=xxx&width=xx&height=xx&quality=0.5
(参见 http://g.co/ng/security#xss )
我的离子信息是:
cli包:( / usr / local / lib / node_modules
)
@ionic/cli-utils : 1.15.2
ionic (Ionic CLI) : 3.15.2
全球套餐:
cordova (Cordova CLI) : 7.1.0
本地套餐:
@ionic/app-scripts : 3.0.1
Cordova Platforms : android 6.3.0 ios 4.5.2
Ionic Framework : ionic-angular 3.8.0
系统:
ios-deploy : 1.9.2
ios-sim : 5.0.13
Node : v7.4.0
npm : 5.5.1
OS : macOS Sierra
Xcode : Xcode 8.3.2 Build version 8E2002
有人请帮忙吗?谢谢!!
Anybody please help?? Thanks!!
推荐答案
这就像一个魅力: -
This works like a charm:-
如果您使用的是wkwebview(现在是新应用程序的默认值)
,请按照以下步骤操作,
If you are using wkwebview(which is now default for new apps) then follow below steps,
import { normalizeURL} from ‘ionic-angular’;
store your image path in some new variable.
var imagePath = this.file.dataDirectory + "test.png";
this.tempImagePath = normalizeURL(imagePath);
then in your html file,
use tempImagePath as image src.
资料来源:
https://forum.ionicframework.com/t/downloaded-image-is-not-displayed -ionic-3-8 / 110912/2
要查看WKWebview是否安装正确,请尝试将其添加到主应用程序组件中:
To see if WKWebview is installed correctly, try adding this to your main app component:
if (window.indexedDB) {
console.log('I have WKWebview installed!');
} else {
console.log('I have UIWebView installed!');
}
这篇关于离子3照片库图像未显示在img标签上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!