image
@nativescript-community/ui-image
Advanced and efficient image display plugin which uses Glide (Android) and SDWebImage (iOS) to implement caching, placeholders, image effects, and much more.
 |
 |
| iOS Demo |
Android Demo |
Table of Contents
Installation
Run the following command from the root of your project:
ns plugin add @nativescript-community/ui-image
setup
import imageModule = require("@nativescript-community/ui-image");
//do this before creating any image view
imageModule.initialize();
initialize accepts an optional config:
useOkhttp (Android): a custom okhttp3.OkHttpClient used to download images.
onInitialize (Android and iOS): called before the native library is initialized, for very custom native handling.
Android gets (glideConfiguration, config), iOS gets (loadersManager, loaders, config).
Android: OkHttp is optional
Images are downloaded with OkHttp by default. To not ship it, set this in the app gradle.properties
(or ext.glideUseOkHttp = "false" in before-plugins.gradle):
glideUseOkHttp=false
Images are then downloaded with HttpURLConnection: headers, progress and loadSource events still work,
but useOkhttp is ignored and you lose OkHttp's HTTP/2 and connection pooling.
API
Events
finalImageSet - arguments FinalEventData
This event is fired after the final image has been set. When working with animated images you could use this event to start the animation by calling the FinalEventData.animatable.start() function.
<@nativescript-community/ui-image:Img finalImageSet="onFinalImageSet"/>
JavaScript:
function onFinalImageSet(args) {
var imageModuleEventData = args;
var img = imageModuleEventData.object;
}
exports.onFinalImageSet = onFinalImageSet;
TypeScript:
import {Img, FinalEventData } from "@nativescript-community/ui-image";
export function onFinalImageSet(args: FinalEventData) {
var img = args.object as Img;
}
- failure - arguments FailureEventData
This event is fired after the fetch of the final image failed.
<@nativescript-community/ui-image:Img failure="onFailure"/>
JavaScript:
function onFailure(args) {
var img = args.object;
}
exports.onFailure = onFailure;
TypeScript:
import {Img, FailureEventData } from "@nativescript-community/ui-image";
export function onFailure(args: FailureEventData) {
var img = args.object as Img;
}
- intermediateImageSet - arguments IntermediateEventData (Android only)
This event is fired after any intermediate image has been set.
<@nativescript-community/ui-image:Img intermediateImageSet="onIntermediateImageSet"/>
JavaScript:
function onIntermediateImageSet(args) {
var img = args.object;
}
exports.onIntermediateImageSet = onIntermediateImageSet;
TypeScript:
import {Img, IntermediateEventData } from "@nativescript-community/ui-image";
export function onIntermediateImageSet(args: IntermediateEventData) {
var img = args.object as Img;
}
- intermediateImageFailed - arguments FailureEventData (Android only)
This event is fired after the fetch of the intermediate image failed.
<@nativescript-community/ui-image:Img intermediateImageFailed="onIntermediateImageFailed"/>
JavaScript:
function intermediateImageFailed(args) {
var img = args.object;
}
exports.intermediateImageFailed = intermediateImageFailed;
TypeScript:
import {Img, FailureEventData } from "@nativescript-community/ui-image";
export function intermediateImageFailed(args: FailureEventData) {
var img = args.object as Img;
}
- submit - arguments EventData (Android only)
This event is fired before the image request is submitted.
<@nativescript-community/ui-image:Img submit="onSubmit"/>
JavaScript:
function onSubmit(args) {
var img = args.object;
}
exports.onSubmit = onSubmit;
TypeScript:
import {Img, EventData } from "@nativescript-community/ui-image";
export function onSubmit(args: EventData) {
var img = args.object as Img;
}
- release - arguments EventData (Android only)
This event is fired after the controller released the fetched image.
<@nativescript-community/ui-image:Img release="onRelease"/>
JavaScript:
function onRelease(args) {
var img = args.object;
}
exports.onRelease = onRelease;
TypeScript:
import {Img, EventData } from "@nativescript-community/ui-image";
export function onRelease(args: EventData) {
var img = args.object as Img;
}
Event arguments
Instances of this class are provided to the handlers of the finalImageSet.
import {Img, FinalEventData, ImageInfo, AnimatedImage } from "@nativescript-community/ui-image";
export function onFinalImageSet(args: FinalEventData) {
var info: ImageInfo = args.imageInfo;
var animatable: AnimatedImage = args.animatable;
var quality: number = info.getQualityInfo().getQuality();
var isFullQuality: boolean = info.getQualityInfo().isOfFullQuality();
var isOfGoodEnoughQuality: boolean = info.getQualityInfo().isOfGoodEnoughQuality();
}
Instances of this class are provided to the handlers of the failure and intermediateImageFailed.
import {Img, FailureEventData, imageModuleError } from "@nativescript-community/ui-image";
export function onFailure(args: FailureEventData) {
var error: imageModuleError = args.error;
var message: string = error.getMessage();
var type: string = error.getErrorType();
var fullError: string = error.toString();
}
Instances of this class are provided to the handlers of the intermediateImageSet.
import {Img, IntermediateEventData, ImageInfo } from "@nativescript-community/ui-image";
export function onIntermediateImageSet(args: IntermediateEventData) {
var info: ImageInfo = args.imageInfo;
var quality: number = info.getQualityInfo().getQuality();
var isFullQuality: boolean = info.getQualityInfo().isOfFullQuality();
var isOfGoodEnoughQuality: boolean = info.getQualityInfo().isOfGoodEnoughQuality();}
Instances of this class are provided to the handlers of the release and submit.
import {Img, EventData } from "@nativescript-community/ui-image";
export function onSubmit(args: EventData) {
var img = args.object as Img;
}
Properties
String value used for the image URI. You can use this property to set the image to be loaded from remote location (http, https), from the resources and local files of your {N} application.
It can also be an ImageSource/ImageAsset, a Promise, or a function called with the view as argument and returning one of those (or a Promise of those).
<@nativescript-community/ui-image:Img src="https://docs.nativescript.org/angular/img/cli-getting-started/angular/chapter0/NativeScript_logo.png"/>
String value used for the placeholder image URI. You can use this property to set a placeholder image loaded from the local and resources files of your {N} application.
*Note: Currently there are limitations on how many different Images can be set to as 'placeholderImage' before OutOfMemoryError is thrown. For best results its recommended to use a single image for all placeholderImageUri of your Img instances.
<@nativescript-community/ui-image:Img placeholderImageUri="~/placeholder.jpg"/>
String value used for the failure image URI. You can use this property to set a failure image loaded from the local and resources files of your {N} application that will be shown if the loading of the src is not successful.
<@nativescript-community/ui-image:Img failureImageUri="~/failure.jpg"/>
Advanced optional attributes
There are a couple of optional attributes that could be set on the Img instance to achieve advanced behaviors:
- backgroundUri (Android only)
String value used for the background image URI. Using this property has similar effect as the placeholderImageUri but the image is stretched to the size of the Img.
*Note: Currently there are limitations on how many different Images can be set to as 'background' before OutOfMemoryError is thrown. For best results its recommended to use a single image for all backgroundUri of your Img instances.
<@nativescript-community/ui-image:Img backgroundUri="~/image.jpg"/>
String value used by Img image scale type. This property can be set to:
'center' - Performs no scaling.
'centerCrop' - Scales the child so that both dimensions will be greater than or equal to the corresponding dimension of the parent.
'centerInside' - Scales the child so that it fits entirely inside the parent.
'fitCenter' - Scales the child so that it fits entirely inside the parent.
'aspectFit' - Scales the child so that it fits entirely inside the parent.
'fitStart' - Scales the child so that it fits entirely inside the parent.
'fitEnd' - Scales the child so that it fits entirely inside the parent.
'fitXY' - Scales width and height independently, so that the child matches the parent exactly.
'fill' - Scales width and height independently, so that the child matches the parent exactly.
'focusCrop' - Scales the child so that both dimensions will be greater than or equal to the corresponding dimension of the parent.
'aspectFill' - Scales the child so that both dimensions will be greater than or equal to the corresponding dimension of the parent.
<@nativescript-community/ui-image:Img stretch="centerInside"/>
Number value used for the fade-in duration. This value is in milliseconds.
<@nativescript-community/ui-image:Img fadeDuration="3000"/>
Number value greater than zero, used as input for the blur function. Larger value means slower processing. For example a value of 10 means that each pixel in the image will be blurred using all adjacent pixels up to a distance of 10.
<@nativescript-community/ui-image:Img blurRadius="25"/>
- blurDownSampling (Android only)
Number value greater than zero, used to scale the image before applying the blur function. Larger value means faster processing. For example a value of 2 means that the image will be scaled by a factor of two before applying blur.
<@nativescript-community/ui-image:Img blurDownSampling="2"/>
Number value used as the aspect ratio of the image. This property is useful when you are working with different aspect ratio images and want to have a fixed Width or Height. The ratio of an image is calculated by dividing its width by its height.
Note: In some layout scenarios it is necessary to set the verticalAlignment of the Img to 'top' or 'bottom' in order to "anchor" the img and achieve dynamic sizing.
<@nativescript-community/ui-image:Img aspectRatio="1.33" verticalAlignment="top"/>
- decodeWidth (downsampling)
Number value used as the downsampled width of the imageModule drawable.
<@nativescript-community/ui-image:Img decodeWidth="100"/>
- decodeHeight (downsampling)
Number value used as the downsampled width of the imageModule drawable.
<@nativescript-community/ui-image:Img decodeHeight="100"/>
Object that defines custom request headers to be sent with the image download request.
It can also be a function returning the headers, called each time the image is requested (for example to always send a fresh token).