如何在nativescript中缓存图像
我创建了一个应用程序,它确实很有效,但每次我上下滚动时都会加载图像,我在谷歌中查看它,发现问题是我使用angular,在我的angular版本中没有像如何在nativescript中缓存图像,nativescript,Nativescript,我创建了一个应用程序,它确实很有效,但每次我上下滚动时都会加载图像,我在谷歌中查看它,发现问题是我使用angular,在我的angular版本中没有像viewModel这样的东西,我如何将缓存图像应用到模板中?你可以试试这段代码。它对我有用。 You can try this code. It is working for me. .ts code import imageCacheModule = require("ui/image-cache"); import imageSourc
viewModel
这样的东西,我如何将缓存图像应用到模板中?你可以试试这段代码。它对我有用。
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>
.ts代码
导入imageCacheModule=require(“ui/图像缓存”);
导入图像源=需要(“图像源”);
var cache=新的imageCacheModule.cache();
private _imageSrc:任何;
私有imgSource:任何;
getImageCache(imageURL){
cache.placeholder=imageSource.fromResource(“res://no-image.png");
cache.maxRequests=10;
cache.enableDownload()
var images=cache.get(imageURL)
如果(图像){
返回图像
}否则{
cache.push({
关键字:imageURL,
url:imageURL,
已完成:(图像:任意,键:字符串)=>{
如果(imageURL==键){
this.imgSource=imageSource.fromNativeSource(图像);
}
}
})
cache.disableDownload();
}
HTMl代码
这是如何工作的?else
part没有任何return
语句。那私有的imgSource
属性是什么?我很困惑。在else中,“this.imgSource”将更新映像。您可以添加控制台并检查。