如何使用ImageCache Nativescript核心模块

时间:2019-05-01 15:36:02

标签: android ios vue.js nativescript nativescript-vue

我正在尝试使用ImageCache NativeScript Core模块从缓存中保存和加载图像,但是它将无法正常工作。

<template>
 <Page>
  <StackLayout>
   <Image v-for="exampleImage in exampleImages" :src="getCachedImage(exampleImage.url)"/>
  </StackLayout>
 </Page>
</template>

<script>
 import * as imageCache from 'tns-core-modules/ui/image-cache'
 import * as imageSource from 'tns-core-modules/image-source'

 export defualt {
  data() {
   return {
    exampleImages: [
     {url: 'https://image.tmdb.org/t/p/w600_and_h900_bestv2/kY2c7wKgOfQjvbqe7yVzLTYkxJO.jpg'},
     {url: 'https://image.tmdb.org/t/p/w600_and_h900_bestv2/svIDTNUoajS8dLEo7EosxvyAsgJ.jpg'},
     {url: 'https://image.tmdb.org/t/p/w600_and_h900_bestv2/A7XkpLfNH0El2yyDLc4b0KLAKvE.jpg'},
    ]
   }
  },
  methods: {
   getCachedImage(imgUrl) {
                const cache = new imageCache.Cache();
                cache.enableDownload();

                const image = cache.get(imgUrl);
                let cachedImageSource;
                if (image) {
                    console.log('getting image from cache')
                    cachedImageSource = imageSource.fromNativeSource(image)
                } else {
                    console.log('downloading image, setting it in cache, and getting from cache')

                    cache.push({
                        key: imgUrl,
                        url: imgUrl,
                        completed: (image, key) => {
                            if (imgUrl === key) {
                                cachedImageSource = imageSource.fromNativeSource(image);
                                console.log(cachedImageSource)
                            }
                        },
                        error: () => {
                            console.log('Error')
                        }
                    });
                }
                cache.disableDownload();
                return cachedImageSource;
            }
  }
 }
</script>

但是,控制台中的输出如下:

iOS:

{ ios: {} }

Android:

{ android:
    { constructor:
       { [Function]
         [length]: 0,
         [name]: '',
         [arguments]: null,
         [caller]: null,
         [prototype]: [Object],
         createBitmap: [Object],
         createScaledBitmap: [Object],
         extend: [Object],
         CREATOR: [Object],
         DENSITY_NONE: 0,
         CONTENTS_FILE_DESCRIPTOR: 1,
         PARCELABLE_WRITE_RETURN_VALUE: 1,
         null: [Circular],
         class: [Object],
         CompressFormat: [Object],
         Config: [Object] } } }

当然总是输出:downloading image, setting it in cache, and getting from cache,从不输出getting image from cache。该图像从不显示,从不保存在缓存中,也从未从缓存中获取。

我不知道自己在做什么错。

谢谢。

1 个答案:

答案 0 :(得分:1)

图像下载是异步的,因此您不能使用直接返回语句。您必须等待完整的回调并使用图像URL更新数据。

<template>
    <Page class="page">
        <ActionBar title="Home" class="action-bar" />
        <ScrollView>
            <StackLayout>
                <Image v-for="exampleImage in exampleImages" :src="exampleImage.src" />
            </StackLayout>
        </ScrollView>
    </Page>
</template>

<script>
    import * as imageCache from "tns-core-modules/ui/image-cache";
    import * as imageSource from "tns-core-modules/image-source";
    export default {
        data() {
            return {
                exampleImages: [{
                        url: "https://image.tmdb.org/t/p/w600_and_h900_bestv2/kY2c7wKgOfQjvbqe7yVzLTYkxJO.jpg",
                        src: null
                    },
                    {
                        url: "https://image.tmdb.org/t/p/w600_and_h900_bestv2/svIDTNUoajS8dLEo7EosxvyAsgJ.jpg",
                        src: null
                    },
                    {
                        url: "https://image.tmdb.org/t/p/w600_and_h900_bestv2/A7XkpLfNH0El2yyDLc4b0KLAKvE.jpg",
                        src: null
                    }
                ]
            };
        },
        methods: {
            getCachedImage(exampleImage) {
                const cache = new imageCache.Cache();
                cache.enableDownload();
                const image = cache.get(exampleImage.url);
                let cachedImageSource;
                if (image) {
                    console.log("getting image from cache");
                    exampleImage.src = imageSource.fromNativeSource(image);
                } else {
                    console.log(
                        "downloading image, setting it in cache, and getting from cache"
                    );
                    cache.push({
                        key: exampleImage.url,
                        url: exampleImage.url,
                        completed: (image, key) => {
                            exampleImage.src = imageSource.fromNativeSource(
                                image);
                        },
                        error: () => {
                            console.log("Error");
                        }
                    });
                }
                // cache.disableDownload();
            }
        },
        created() {
            for (let x in this.exampleImages) {
                this.getCachedImage(this.exampleImages[x]);
            }
        }
    };
</script>

Updated Playground