Ionic Module that loads images in a background thread and caches them for later use. Uses HttpClient
from Angular 4+
, and cordova-plugin-file
via ionic-native
wrappers.
View our example project here: https://github.com/zyramedia/ionic-image-loader-example
npm install --save ionic-image-loader
npm i --save @ionic-native/file
ionic cordova plugin add cordova-plugin-file
IonicImageLoader
moduleAdd IonicImageLoader.forRoot()
in your app's root module
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)
import { IonicImageLoader } from 'ionic-image-loader';
@NgModule({
...
imports: [
IonicImageLoader
]
})
export class SharedModule {}
This HTML code demonstrates basic usage of this module:
<img-loader src="https://path.to/my/image.jpg"></img-loader>
By default, the module sets the image as the background of the <img-loader>
element. If you want the module to use the image as an <img>
tag inside the <img-loader>
element, just add useImg
attribute as shown below:
<img-loader src="https://path.to/my/image.jpg" useImg></img-loader>
You can also listen to the load event to be notified when the image has been loaded:
<img-loader src="https://github.com/zyra/ionic-image-loader/raw/master/path/to/image" (load)="onImageLoad($event)></img-loader>
import { ImgLoaderComponent } from 'ionic-image-loader';
...
onImageLoad(imgLoader: ImgLoaderComponent) {
// do something with the loader
}
The <img-loader>
component takes many attributes that allows you to customize the image. You can use the following table as a reference:
Attribute Name | Type | Description | Default Value |
---|---|---|---|
src | string | The image URL | N/A |
fallbackUrl | string | Fallback image url to load in case the original image fails to load | N/A |
spinner | boolean | Show a spinner while the image loads | true |
useImg | boolean | Use <img> tag to display the image in |
false |
width | string | The width of the image. This is ignored if useImg is set to true . |
100% |
height | string | The height of the image. This is ignored if useImg is set to true . |
100% |
display | string | Sets the display CSS property of the <img-loader> element. This is ignored if useImg is set to true . |
block |
backgroundSize | string | Sets the background-size CSS property of the <img-loader> element. This is ignored if useImg is set to true . |
contain |
backgroundRepeat | string | Sets the background-repeat CSS property of the <img-loader> element. This is ignored if useImg is set to true . |
no-repeat |
fallbackAsPlaceholder | boolean | Use fallback as a placeholder until the image loads. | false |
imgAttributes | ImageAttribute[] | An array of ImageAttribute objects (element, value). If useImg == true , this array will be iterated and each object added as an attribute to the generated <img> HTML element. |
[]] |
Note: The default values can be changed using the global configuration feature.
In some cases, images won't load on the first time, the culprit seems to be @ionic-native/file
or cordova-plugin-file
in its writeFile
function not calling resolve or reject.
In the meantime we find a solution, here's a quick workaround:
In ./src/index.html move your polyfill.js
include above cordova.js
<!-- The polyfills js is generated during the build process -->
<script src="https://github.com/zyra/ionic-image-loader/raw/master/build/polyfills.js"></script>
<!-- cordova.js required for cordova apps (remove if not needed) -->
<script src="https://github.com/zyra/ionic-image-loader/raw/master/cordova.js"></script>
This is optional but it is helpful if you wish to set the global configuration for all of your <img-loader>
instances. To configure the module, inject the ImageLoaderConfig
provider in your app's main component.
import { ImageLoaderConfig } from 'ionic-image-loader';
@Component({
...
})
export class MyMainAppComponent {
constructor(
private imageLoaderConfig: ImageLoaderConfig // optional, if you wish to configure the service
){
// disable spinners by default, you can add [spinner]="true" to a specific component instance later on to override this
imageLoaderConfig.enableSpinner(false);
// set the maximum concurrent connections to 10
imageLoaderConfig.setConcurrency(10);
}
}
Below are all the methods that the config provider has:
Enables debug mode to receive console logs, errors, warnings.
Example:
// enable debug mode to get console errors/warnings/logs
// this could be useful while trying to debug issues with the component
this.imageLoaderConfig.enableDebugMode();
Sets the cache directory name. Defaults to 'image-loader-cache'. Defaults to true
.
Example:
this.imageLoaderConfig.enableSpinner(false); // disable spinner by default
Set default height for images that are not using tag. Defaults to 100%
.
Set default width for images that are not using tag. Defaults to 100%
.
Example:
this.imageLoaderConfig.setWidth('500px'); // set default width to 500px
Enable display mode for images that are not using tag. Defaults to block
.
Example:
this.imageLoaderConfig.setDisplay('inline-block');
Use tag by default.
Example:
this.imageLoaderConfig.useImageTag(true); // use `<img>` tag by default
Set default background size for images that are not using tag. Defaults to contain
.
Example:
this.imageLoaderConfig.setBackgroundSize('cover');
Set background repeat for images that are not using tag. Defaults to no-repeat
.
Example:
this.imageLoaderConfig.setBackgroundRepeat('repeat-x');
Set fallback URL to use when image src is undefined or did not resolve. This image will not be cached. This should ideally be a locally saved image.
Example:
this.imageLoaderConfig.setFallbackUrl('assets/fallback.png'); // if images fail to load, display this image instead
Set a custom directory name to store the cached images in. The cache directory by default is named image-loader-cache
.
Example:
this.imageLoaderConfig.setCacheDirectoryName('my-custom-cache-directory-name');
Set the maximum number of concurrent connections. Cached images will be loaded instantly, this limit is only for new images.
Example:
this.imageLoaderConfig.setConcurrency(5); // 5 concurrent connections
Sets the maximum cache size in bytes.
Example:
this.imageLoaderConfig.setMaximumCacheSize(20 * 1024 * 1024); // set max size to 20MB
Sets the maximum allowed cache age in milliseconds
Example:
this.imageLoaderConfig.setMaximumCacheAge(7 * 24 * 60 * 60 * 1000); // 7 days
Set the return type of cached images. By default this option is set to 'uri', which will return the native file URL. If you want to get a base64-encoded representation of the file set the return type to 'base64'.
Example:
this.imageLoaderConfig.setImageReturnType('base64');
Enable/Disable the fallback image as placeholder instead of the spinner. Defaults to false.
Example:
this.imageLoaderConfig.enableFallbackAsPlaceholder(true);
Set headers for HttpClient to use.
Example:
const headers = new HttpHeaders()
.set("Authorization", "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==");
this.imageLoaderConfig.setHttpHeaders(headers);
Enable/Disable the save filename of cached images with extension. Defaults to false.
Example:
this.imageLoaderConfig.setFileNameCachedWithExtension(true);
Sometime url missing extension, in this case you can set fallback as default extension. Defaults to '.jpg'
Example:
this.imageLoaderConfig.setFallbackFileNameCachedExtension('.png');
import { ImageLoader } from 'ionic-image-loader';
class MyComponent {
constructor(imageLoader: ImageLoader) {
imageLoader.preload('http://path.to/image.jpg');
}
}
import { ImageLoader } from 'ionic-image-loader';
@Component(...)
class MyComponent {
constructor(imageLoader: ImageLoader) {
imageLoader.clearCache();
}
}
import { ImageLoader } from 'ionic-image-loader';
@Component(...)
class MyComponent {
constructor(imageLoader: ImageLoader) {
imageLoader.clearImageCache('http://path.to/image.jpeg');
}
}
When using ImageLoader to generate an <img>
element it may be desirable for the generated element to include additional attributes to provide styling or interaction qualities. The optional imgAttributes
value can be used to provide such additional attributes which will be included in the generated <img>
element in the DOM.
Usage:
Include the ImageAttribute model in your .ts
import { ImageAttribute } from 'ionic-image-loader'
Generate an array of ImageAttribute objects
const imageAttributes: ImageAttribute[] = [];
imageAttributes.push({
element: 'class',
value: 'circle-photo'
})
Include the imgAttributes
element in the img-loader
element of your HTML
<img-loader [src]="..." useImg [imgAttributes]="imageAttributes"> </img-loader>
The generated <img>
tag will be rendered with the specified attributes
<img src="https://github.com/zyra/ionic-image-loader/raw/master/.." class="circle-photo">
If you find this project useful, please star the repo to let people know that it's reliable. Also, share it with friends and colleagues that might find this useful as well. Thank you :smile: