【问题标题】:how do i cache images in nativescript angular我如何在 nativescript angular 中缓存图像
【发布时间】:2018-11-18 09:01:12
【问题描述】:

我创建了一个应用程序,它运行良好,但每次我上下滚动时都会加载图像,我在 google 中查找并找到 this,问题是我使用 angular,没有 viewModel 这样的东西在我的角度版本中,如何将缓存图像应用到模板中?

【问题讨论】:

    标签: nativescript


    【解决方案1】:
    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 属性是什么?我很困惑。
    • In else 'this.imgSource' 将更新图像。您可以添加控制台并检查。
    猜你喜欢
    • 2020-08-08
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2017-09-24
    • 2018-06-17
    • 2012-03-08
    • 2015-12-11
    相关资源
    最近更新 更多