NativeScript Fresco
A NativeScript Plugin for Android apps.
This plugin is deprecated. Feel free to use the Image Plugin for efficiently displaying images on Android and iOS in your NativeScript app. If you already have an app that use the Fresco Plugin, read the migrate-to-image doc for initial guidance.
- NativeScript Fresco
nativescript-fresco
?
What is nativescript-fresco
is a NativeScript plugin that exposes the Fresco library used for efficiently displaying images on Android. More information about what Fresco is and how it works is available on its official website here.
The nativescript-fresco
plugin enables NativeScript developers to use the FrescoDrawee
class which is extends the traditional Android ImageView component and adds the smart Fresco image management algorithms. The plugin exposes the drawee as a NativeScript view so you basically put it in the XML definition of your page and provide the URI to the image you would like to use.
nativescript-fresco
?
How to use In vanila NativeScript
From npm
- Go to the root folder of your {N} application where you would like to install the plugin and type
tns plugin add nativescript-fresco
. - Initialize
nativescript-fresco
in thelaunch
event of your {N} application by using the following code:
From local repo?
- Clone the repository and go to the root directory on your computer.
- Use
tsc
to transpile the.ts
sources:tsc -p
. - Go to the root folder of your {N} application where you would like to install the plugin and type
tns plugin add <path-to-fresco-repo-dir>
. - Initialize
nativescript-fresco
in thelaunch
event of your {N} application by using the following code:
JavaScript:
var application = ;var fresco = ; if applicationandroid application;
TypeScript:
;; if application.android
When working with "downsampling" you will need to pass a configuration to the
initialize
function:
fresco;
Use fresco
in the XML definition of the page as follows:
In NativeScript + Angular 2
- Import the
TNSFrescoModule
fromnativescript-fresco/angular
and add it to theimports
of your initial@NgModule
, like shown here. - As described above make sure to initialize the
nativescript-fresco
plugin in thelaunch
event of your {N} application.
Examples
You can refer the demo-angular folder of the repo for runnable {N} project that demonstrates the nativescript-fresco plugin with all of its features in action.
Migrating from 3.x.x to 4.x.x
- If you are using the
AnimatedImage
class you can continue to cast to this type just note that it is now an interface rather than a class - If you are using the
IAnimatedImage
interface simply change all of your references to beAnimatedImage
- If you are using the
IImageInfo
interface simply change all of your references to beImageInfo
- If you are using the
IError
interface simply change all of your references to beFrescoError
Features
Native Fresco library typings
If you find yourself in the need to access directly the native library of the nativescript-fresco plugin you can import the android.d.ts from the plugin in your references.d.ts
:
/// <reference path="./node_modules/nativescript-fresco/android.d.ts" />
As documented by the Fresco library setting the height and width are mandatory, more details on this topic could be found here. So the first this you should do when declaring the FrescoDrawee is set its width and height attributes or set only one of them and set the FrescoDrawee's aspectRatio. The width and height of the FrescoDrawee in your {N} application supports percentages which makes it possible to declare for example width="50%" and aspectRatio="1.33" achieving exactly 50% width with dynamically calculated height based on the aspect ration of the loaded image from the imageUri.
Basic attributes
- imageUri
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.
- placeholderImageUri
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 FrescoDrawee instances.
- failureImageUri
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 imageUri is not successful.
Advanced optional attributes
There are a couple of optional attributes that could be set on the FrescoDrawee instance to achieve advanced behaviors:
- backgroundUri
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 FrescoDrawee.
*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 FrescoDrawee instances.
- actualImageScaleType
String value used by FrescoDrawee 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.
'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.
'focusCrop' - Scales the child so that both dimensions will be greater than or equal to the corresponding dimension of the parent.
- fadeDuration
Number value used for the fade-in duration. This value is in milliseconds.
- blurRadius
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.
- blurDownSampling
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.
- aspectRatio
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 FrescoDrawee to 'top' or 'bottom' in order to "anchor" the drawee and achieve dynamic sizing.
- decodeWidth (downsampling) - make sure to enable downsample (isDownsampleEnabled) in the initialize function of the plugin otherwise this property is disregarded.
Number value used as the downsampled width of the fresco drawable.
- decodeHeight (downsampling) - make sure to enable downsample (isDownsampleEnabled) in the initialize function of the plugin otherwise this property is disregarded.
Number value used as the downsampled width of the fresco drawable.
- progressiveRenderingEnabled
Boolean value used for enabling or disabling the streaming of progressive JPEG images. This property is set to 'false' by default. Setting this property to 'true' while loading JPEG images not encoded in progressive format will lead to a standard loading of those images.
- showProgressBar
Boolean value used for showing or hiding the progress bar.
- progressBarColor
String value used for setting the color of the progress bar. You can set it to hex values ("#FF0000") and/or predefined colors ("green").
- roundAsCircle
Boolean value used for determining if the image will be rounded as a circle. Its default value is false. If set to true the image will be rounder to a circle.
- roundedCornerRadius
Number value used as radius for rounding the image's corners.
- roundBottomRight
Boolean value used for determining if the image's bottom right corner will be rounded. The roundedCornerRadius is used as the rounding radius.
- roundBottomLeft
Boolean value used for determining if the image's bottom left corner will be rounded. The roundedCornerRadius is used as the rounding radius.
- roundTopLeft
Boolean value used for determining if the image's top left corner will be rounded. The roundedCornerRadius is used as the rounding radius.
- roundTopRight
Boolean value used for determining if the image's top right corner should be rounded. The roundedCornerRadius is used as the rounding radius.
- autoPlayAnimations
Boolean value used for enabling the automatic playing of animated images. Note that rounding of such images is not supported and will be ignored.
- tapToRetryEnabled
Boolean value used for enabling/disabling a tap to retry action for the download of the FrescoDrawee image.
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.
JavaScript:
{ var frescoEventData = args; var drawee = frescoEventDataobject;}exportsonFinalImageSet = onFinalImageSet;
TypeScript:
;
- failure - arguments FailureEventData
This event is fired after the fetch of the final image failed.
JavaScript:
{ var drawee = argsobject;}exportsonFailure = onFailure;
TypeScript:
;
- intermediateImageSet - arguments IntermediateEventData
This event is fired after any intermediate image has been set.
JavaScript:
{ var drawee = argsobject;}exportsonIntermediateImageSet = onIntermediateImageSet;
TypeScript:
;
- intermediateImageFailed - arguments FailureEventData
This event is fired after the fetch of the intermediate image failed.
JavaScript:
{ var drawee = argsobject;}exportsintermediateImageFailed = intermediateImageFailed;
TypeScript:
;
- submit - arguments EventData
This event is fired before the image request is submitted.
JavaScript:
{ var drawee = argsobject;}exportsonSubmit = onSubmit;
TypeScript:
;
- release - arguments EventData
This event is fired after the controller released the fetched image.
JavaScript:
{ var drawee = argsobject;}exportsonRelease = onRelease;
TypeScript:
;
Event arguments
All events exposed by 'nativescript-fresco' provide additional information to their handlers that is needed to properly handle them. Here's a brief description of the event arguments coming with each of the events:
- FinalEventData
Instances of this class are provided to the handlers of the finalImageSet.
;
- FailureEventData
Instances of this class are provided to the handlers of the failure and intermediateImageFailed.
;
- IntermediateEventData
Instances of this class are provided to the handlers of the intermediateImageSet.
;
- EventData
Instances of this class are provided to the handlers of the release and submit.
;
Cache
The nativescript-fresco {N} plugin has built-in cache mechanism which handles managing the images in the memory. There are two types of cache mechanisms memory
and disk
, you can manually manage both of them with the following functionality.
'Refresh' the 'imageUri'
Not so rarely you may have a scenario where the actual image on your remote service from the imageUri
of the FrescoDrawee
has changed but the {N} app already has an image in its internal cache. In such scenario you can easily 'refresh' the imageUri
by calling the updateImageUri()
:
// 'drawee' is the instance the 'FrescoDrawee' in the project.
drawee.updateImageUri();
Clear everything from the cache
Managing the caches in nativescript-fresco is done via the ImagePipeline
. In order to get the reference of the ImagePipeline simply call the getImagePipeline()
function:
var frescoModel = require("nativescript-fresco");
var imagePipeLine = frescoModel.getImagePipeline();
- Clear both the memory and disk caches
imagePipeLine.clearCaches();
- Clear the memory cache
imagePipeLine.clearMemoryCaches();
- Clear the disk cache
imagePipeLine.clearDiskCaches();
Evict all images with a specific URI from the cache
If clearing the entire cache is not what you desired, you can clear only the images linked with a specific URI (imageUri
). Evicting is done again via the ImagePipeline:
var frescoModel = require("nativescript-fresco");
var imagePipeLine = frescoModel.getImagePipeline();
- Evict URI from both the memory and disk caches
imagePipeLine.evictFromCache("<uri-to-a-photo-from-the-web>");
- Evict URI from the memory cache
imagePipeLine.evictFromMemoryCache("<uri-to-a-photo-from-the-web>");
- Evict URI from the disk cache
imagePipeLine.evictFromDiskCache("<uri-to-a-photo-from-the-web>");
Manually shut down the native Fresco library
In very very rare occasions the native Android Fresco library may experience strange memory leak issues, in such scenarios as a last resort you may want to "shut down" the library forcing all of the managed memory to possibly be released. You can do that by calling the shutDown
function exposed by the nativescript-fresco module, one good application lifecycle event to call it inside may be in the exit
event of the application:
;; if appandroid app;
Sample Screenshots
All of the images are sample images for showcasing purposes.
Sample 1 - Placeholder image | Sample 2 - Transition (fade-in animation) |
---|---|
![]() |
![]() |
Sample 3 - Image shown successfully from imageUri | Sample 4 - 'Failure' image shown |
---|---|
![]() |
![]() |
Contribute
We love PRs! Check out the contributing guidelines. If you want to contribute, but you are not sure where to start - look for issues labeled help wanted
.
Get Help
Please, use github issues strictly for reporting bugs or requesting features. For general questions and support, check out Stack Overflow or ask our experts in NativeScript community Slack channel.