我如何在 nativescript angular 中缓存图像
问题描述:
我创建了一个应用程序,它运行良好,但每次上下滚动时都会加载图像,我在谷歌中查看并找到了 这个,问题是我没有用angular,有在我的 angular 版本中,诸如 viewModel
之类的东西,我如何将缓存图像应用到模板中?
I created an app and it does work great but images load every time I scroll up and down, I looked it in google and found this, the problem is I use angular, there is no such thing as viewModel
in my angular version, how do I apply cache image into template?
答
You can try this code. It is working for me.
.ts code
import imageCacheModule = require("ui/image-cache");
import imageSource = require("image-source");
var cache = new imageCacheModule.Cache();
private _imageSrc: any;
private imgSource: any;
getImageCache(imageURL) {
cache.placeholder = imageSource.fromResource("res://no-image.png");
cache.maxRequests = 10;
cache.enableDownload()
var images = cache.get(imageURL)
if(images) {
return images
} else {
cache.push({
key: imageURL,
url: imageURL,
completed: (image: any, key: string) => {
if (imageURL === key) {
this.imgSource = imageSource.fromNativeSource(images);
}
}
})
cache.disableDownload();
}
HTMl code
<Image [src]="getImageCache(item.image?.url)" class="item-image"></Image>