ImageCache

Using the ImageCache requires the "ui/image-cache" module.

import * as imageCacheModule from "tns-core-modules/ui/image-cache";
import * as imageSource from "tns-core-modules/image-source";
import * as fs from "tns-core-modules/file-system";

Requesting Images

var cache = new imageCacheModule.Cache();
cache.placeholder = imageSource.fromFile(fs.path.join(__dirname, "res/no-image.png"));
cache.maxRequests = 5;

// Enable download while not scrolling
cache.enableDownload();

var imgSouce: imageSource.ImageSource;
var url = "https://github.com/NativeScript.png";
// Try to read the image from the cache
var image = cache.get(url);
if (image) {
    // If present -- use it.
    imgSouce = imageSource.fromNativeSource(image);
}
else {
    // If not present -- request its download.
    cache.push({
        key: url,
        url: url,
        completed: (image: any, key: string) => {
            if (url === key) {
                imgSouce = imageSource.fromNativeSource(image);
            }
        }
    });
}

// Disable download while scrolling
cache.disableDownload();

Stay connected with NativeScript

(expect a newsletter every 4-8 weeks)

NativeScript
NativeScript is licensed under the Apache 2.0 license .
© Progress Software Corporation. All Rights Reserved.