Ionic3图片库图像无法在img标签中显示

4
我遵循 ionic 文档(这里)和 Github 文档(这里)上的指示。到目前为止,我可以检索所有库项目信息。但是我从未成功在img标签中显示照片。
以下是我的 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;
}

}

这是home.html文件:

<ion-content padding>
  <img *ngFor="let libraryItem of library" [src]="imgUrl(libraryItem.thumbnailURL)" />
</ion-content>

我总是得到一个空的img框,其src=null。 如果我不使用DomSantinizer,它将为所有照片显示警告:

WARNING: sanitizing unsafe URL value cdvphotolibrary://thumbnail?photoId=xxx&width=xx&height=xx&quality=0.5

(见http://g.co/ng/security#xss)
我的ionic信息是: cli packages:(/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 

有人可以帮忙吗?谢谢!


你是否在你的index.html文件中添加了<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: cdvphotolibrary:">标签? - dlcardozo
@DiegoCardozo 是的,我试过了。在index.html中添加了这行代码后,应用程序只能运行白屏,没有其他内容。我不确定问题出在哪里... - Victoria
你的config.xml文件中加入以下标签:<allow-intent href="cdvphotolibrary:*" />。 - dlcardozo
@DiegoCardozo 在将上述代码添加到config.xml后,没有任何变化... - Victoria
我再次添加了console.log(this.sanitizer.bypassSecurityTrustUrl(imgUrl)),但仍然返回{"changingThisBreaksApplicationSecurity":"cdvphotolibrary://thumbnail?photoId=xxx&width=xx&height=xx&quality=0.5"}。有什么想法吗? - Victoria
Android测试正常。只有在IOS上无法工作。 - Victoria
2个回答

2
我认为我在这里找到了一个解决方案——将WKWebView更改为UIWebView。然后图片就会显示出来。
我在这篇帖子中找到了原因。Ionic官方的文档也证明,目前WKWebView不支持本地文件。
我希望我的帖子能够帮助到有类似情况的人,并且希望Ionic团队能够尽快改进WKWebView的自定义模式文件路径。愉快的编码~!

1
这个方法非常有效:
如果您正在使用wkwebview(现在是新应用程序的默认选项),则请按照以下步骤进行操作:
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.

Source: https://forum.ionicframework.com/t/downloaded-image-is-not-displayed-ionic-3-8/110912/2

要检查WKWebView是否正确安装,请尝试将以下内容添加到您的主应用程序组件中:
if (window.indexedDB) {
  console.log('I have WKWebview installed!');
} else {
  console.log('I have UIWebView installed!');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接