【发布时间】:2019-06-03 04:11:32
【问题描述】:
使用 nativescript.org 网站上提供的示例 我创建了一个页面来测试图像缓存组件。 tns-core-modules/ui/image-cache
cache.push() 工作正常。 任何时候我使用 cache.get(url) 对象都会返回 null。我永远无法检索缓存的图像。
在推送完成后,似乎可以从缓存中检索图像,但似乎无法保留。
这发生在模拟器以及实际连接的安卓设备上。
我尝试从头开始创建一个空的地狱世界 JavaScript 应用程序 tns create --template tns-template-hello-world 来测试样本并遇到同样的问题。
const cache = new Cache();
cache.placeholder = fromFile("~/images/logo.png");
cache.maxRequests = 5;
// set the placeholder while the desired image is donwloaded
viewModel.set("imageSource", cache.placeholder);
// Enable download while not scrolling
cache.enableDownload();
let cachedImageSource;
const url = "https://avatars1.githubusercontent.com/u/7392261?v=4";
// Try to read the image from the cache
const image = cache.get(url);
if (image) {
// If present -- use it.
cachedImageSource = imageSource.fromNativeSource(image);
viewModel.set("imageSource", cachedImageSource);
} else {
// If not present -- request its download + put it in the cache.
cache.push({
key: url,
url: url,
completed: (image, key) => {
if (url === key) {
cachedImageSource = fromNativeSource(image);
viewModel.set("imageSource", cachedImageSource); // set the downloaded iamge
}
}
});
}
// Disable download while scrolling
cache.disableDownload();
【问题讨论】:
标签: image caching nativescript