Android 在NativeScript的StackLayout或ListView中使用ImageCache
我有一个项目列表,每个项目都有一个图像。我想缓存图像 因此,我在XML中大致有如下内容:Android 在NativeScript的StackLayout或ListView中使用ImageCache,android,nativescript,Android,Nativescript,我有一个项目列表,每个项目都有一个图像。我想缓存图像 因此,我在XML中大致有如下内容: <StackLayout> <GridLayout> <gv:GridView items="{{ kats }}" verticalSpacing="0" horizontalSpacing="0" colWidth="{{ colWidth }}" rowHeight="10
<StackLayout>
<GridLayout>
<gv:GridView items="{{ kats }}" verticalSpacing="0" horizontalSpacing="0"
colWidth="{{ colWidth }}" rowHeight="100" padding="0"
itemTap="openKat" itemLoading="mainLoading">
<gv:GridView.itemTemplate>
<GridLayout columns="*" rows="80,20">
<Image row="0" src="{{ Image, Image | mkThumbLink() }}" />
<Label row="1" text="{{ description }}" />
</GridLayout>
</gv:GridView.itemTemplate>
</gv:GridView>
</GridLayout>
</StackLayout>
页面加载中的每个图像都会调用toView方法,但我不知道如何在图像到达时将其推回到页面。在“cache.push.completed”部分,我想?我把这个留给那些仍在寻找答案的人。 这就是我的工作。我用打字稿
public get itemImage() : imageSource.ImageSource {
if (constants.IS_DEBUG) console.log("Attempting to fetch image for: " + this.key);
var cache = new imageCacheModule.Cache();
cache.placeholder = imageSource.fromResource("background");
cache.maxRequests = 2;
var imgSource: imageSource.ImageSource = cache.placeholder;
if (this.imageExists && this.itemImageUrl !== '') {
// Try to read the image from the cache
var image = cache.get(this.itemImageUrl);
if (image) {
// If present -- use it.
if (constants.IS_DEBUG) console.log("Image found in cache");
imgSource = imageSource.fromNativeSource(image);
}
else {
// If not present -- request its download.
if (constants.IS_DEBUG) console.log("Image not cached, downloading...");
cache.enableDownload();
cache.enqueue({
key: this.itemImageUrl,
url: this.itemImageUrl,
completed: (image: any, key: string) => {
if (constants.IS_DEBUG) console.log("Image downloaded");
cache.disableDownload();
if (this.itemImageUrl === key) {
imgSource = imageSource.fromNativeSource(image);
this.set('itemImage', imgSource);
}
}
});
}
}
return imgSource;
}
做这项工作的行是this.set('itemImage',imgSource)代码>我把这个留给那些仍在寻找答案的人。 这就是我的工作。我用打字稿
public get itemImage() : imageSource.ImageSource {
if (constants.IS_DEBUG) console.log("Attempting to fetch image for: " + this.key);
var cache = new imageCacheModule.Cache();
cache.placeholder = imageSource.fromResource("background");
cache.maxRequests = 2;
var imgSource: imageSource.ImageSource = cache.placeholder;
if (this.imageExists && this.itemImageUrl !== '') {
// Try to read the image from the cache
var image = cache.get(this.itemImageUrl);
if (image) {
// If present -- use it.
if (constants.IS_DEBUG) console.log("Image found in cache");
imgSource = imageSource.fromNativeSource(image);
}
else {
// If not present -- request its download.
if (constants.IS_DEBUG) console.log("Image not cached, downloading...");
cache.enableDownload();
cache.enqueue({
key: this.itemImageUrl,
url: this.itemImageUrl,
completed: (image: any, key: string) => {
if (constants.IS_DEBUG) console.log("Image downloaded");
cache.disableDownload();
if (this.itemImageUrl === key) {
imgSource = imageSource.fromNativeSource(image);
this.set('itemImage', imgSource);
}
}
});
}
}
return imgSource;
}
做这项工作的行是this.set('itemImage',imgSource)代码>嘿,问题有进展吗?嘿,问题有进展吗?