【发布时间】:2018-11-18 09:01:12
【问题描述】:
我创建了一个应用程序,它运行良好,但每次我上下滚动时都会加载图像,我在 google 中查找并找到 this,问题是我使用 angular,没有 viewModel 这样的东西在我的角度版本中,如何将缓存图像应用到模板中?
【问题讨论】:
标签: nativescript
我创建了一个应用程序,它运行良好,但每次我上下滚动时都会加载图像,我在 google 中查找并找到 this,问题是我使用 angular,没有 viewModel 这样的东西在我的角度版本中,如何将缓存图像应用到模板中?
【问题讨论】:
标签: nativescript
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>
【讨论】:
else 部分没有任何 return 语句。那个私有的imgSource 属性是什么?我很困惑。