1

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?

1 Answer 1

2
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>
Sign up to request clarification or add additional context in comments.

2 Comments

How does this work? else part doesn't have any return statement. And what is that private imgSource property? I'm confused.
In else 'this.imgSource' will update the image. You can add console and check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.