简体   繁体   English

Ionic 2 / Ionic 3 / Ionic 4 : (Lazy) 加载图片微调器

[英]Ionic 2 / Ionic 3 / Ionic 4 : (Lazy) Loading spinner for pictures

I'm using ion-img in my ionic2 application to load my pictures correctly.我在 ionic2 应用程序中使用 ion-img 来正确加载我的图片。 However, I wonder if there is a way to indicate to the user that the picture is actually loading.但是,我想知道是否有一种方法可以向用户表明图片实际上正在加载。

Thank you for your help!感谢您的帮助!

EDIT : Here is the answer if you absolutely need to use the ion-img tag.编辑:如果您绝对需要使用 ion-img 标签,这里是答案。 If not, you should use ionic-image-loader as AdminDev826 suggested.如果没有,您应该按照AdminDev826 的建议使用ionic-image-loader

I finally solved that problem with CSS!我终于用 CSS 解决了这个问题! When an image is loading in ionic 2, the ion-img tag doesn't have any class.当图像在 ionic 2 中加载时, ion-img 标签没有任何类。 However, when the image is finally loaded, the ion-img tag get the class "img-loaded".但是,当最终加载图像时, ion-img 标签获得类“img-loaded”。

Here is my solution :这是我的解决方案:

  <ion-img [src]="img.src"></ion-img>
  <ion-spinner></ion-spinner>

And my CSS :还有我的 CSS :

.img-loaded + ion-spinner {
  display:none;
}

I hope it can help someone!我希望它可以帮助某人!

I finally solved that problem with CSS!我终于用 CSS 解决了这个问题! When an image is loading in ionic 2, the ion-img tag doesn't have any class.当图像在 ionic 2 中加载时, ion-img 标签没有任何类。 However, when the image is finally loaded, the ion-img tag get the class "img-loaded".但是,当最终加载图像时, ion-img 标签获得类“img-loaded”。

Here is my solution :这是我的解决方案:

  <ion-img [src]="img.src"></ion-img>
  <ion-spinner></ion-spinner>

And my CSS :还有我的 CSS :

.img-loaded + ion-spinner {
  display:none;
}

I hope it can help someone!我希望它可以帮助某人!

If you want to use the img tag instead of ion-img here is the solution:如果你想使用img标签而不是ion-img这里是解决方案:

  <img src="{{image.src}}" (load)="loaded = true" [ngClass]="{'img-loaded':loaded}" [hidden]="!loaded" *ngIf="image_exists" />
  <ion-spinner [ngClass]="{'center':true}" *ngIf="!loaded"></ion-spinner>

In your CSS file you should write the following:在您的 CSS 文件中,您应该编写以下内容:

 .img-loaded + ion-spinner {
  display:none;
}
// .center in my case to make the spinner at the center
.center{
  margin-left: auto;
  margin-right: auto;
  display: block;
}

loaded is a boolean variable with false default value you have to define in your component. loaded是一个布尔变量,您必须在组件中定义错误的默认值。

Please use ionic-image-loader plugin请使用 ionic-image-loader 插件

  1. Install the NPM Package安装 NPM 包

    npm install --save ionic-image-loader
  2. Install Required Plugins安装所需的插件

    npm i --save @ionic-native/file ionic plugin add cordova-plugin-file --save npm i --save @ionic-native/transfer ionic plugin add cordova-plugin-file-transfer --save
  3. Import IonicImageLoader module导入 IonicImageLoader 模块

    Add IonicImageLoader.forRoot() in your app's root module在应用的根模块中添加 IonicImageLoader.forRoot()

     import { IonicImageLoader } from 'ionic-image-loader'; // import the module @NgModule({ ... imports: [ IonicImageLoader.forRoot() ] }) export class AppModule {}

    Then add IonicImageLoader in your child/shared module(s)然后在您的子/共享模块中添加 IonicImageLoader

     import { IonicImageLoader } from 'ionic-image-loader'; @NgModule({ ... imports: [ IonicImageLoader ] }) export class SharedModule {}

Your solution is not the best one because the app still downloads all the images, For example in a Facebook like app, You will be downloading all the images from the Feed to your app, This will make everything super slow.您的解决方案不是最好的,因为该应用程序仍会下载所有图像,例如在 Facebook 之类的应用程序中,您将从 Feed 中下载所有图像到您的应用程序,这将使一切变得非常慢。

Use this: https://github.com/NathanWalker/ng2-image-lazy-load使用这个: https : //github.com/NathanWalker/ng2-image-lazy-load

ionic-image-loader not works in ionic4+. ionic-image-loader 不适用于 ionic4+。 You must create a component:您必须创建一个组件:

HTML HTML

<ion-spinner name="dots" [hidden]="viewImage" color="primary"></ion-spinner>
<ion-img #image alt=""(ionImgDidLoad)="loadImage()" (ionError)="errorLoad()"></ion-img>

Typescript打字稿

    @Component({
      selector: 'preloader-img',
      templateUrl: './preloader-img.component.html',
      styles: [`
        ion-spinner {
          display: block;
          margin: auto;
        }
      `],
    })
    export class PreloaderImgComponent implements AfterViewInit {
      viewImage = false;

      @Input() url: string;
      @ViewChild('image', { static: false }) imageRef: IonImg;


      ngAfterViewInit() {
        this.imageRef.src = this.url;
      }
      loadImage() {
        this.viewImage = true;
      }
      errorLoad() {
        this.imageRef.src = '<your_default_img>';
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM