我如何在 nativescript angular 中缓存图像 [英] how do i cache images in nativescript angular

查看:70
本文介绍了我如何在 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>

这篇关于我如何在 nativescript angular 中缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆