android - ImageCache Nativescript核心模块使用方法

标签 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: {} }

安卓:

{ 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] } } }

当然总是输出:下载图像,将其设置在缓存中,然后从缓存中获取,并且从不从缓存中获取图像。图像永远不会显示,永远不会保存在缓存中,也永远不会从缓存中获取。

我不知道我做错了什么。

提前致谢。

最佳答案

图片下载是异步的,所以不能直接使用return语句。您必须等待完整的回调并使用图像 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

关于android - ImageCache Nativescript核心模块使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938785/

相关文章:

javascript - Vue SPA - 如何在浏览器中呈现时隐藏 .vue 文件

ios - 当按下后退按钮时,返回 UINavigationViewController 中的上一个 View 没有动画

objective-c - 来自文档根目录的 Route-Me 离线 map

android - 如何在android中实用地设置图像的layout_width

java - 希望自定义键盘仅用于我的应用程序并在应用程序失去焦点时恢复以前的键盘

iphone - 在 UIView 中绘制垂直线

javascript - Vue 属性定义警告,即使它是在实例上定义的

vue.js - 使用 Vuex 突变更新对象引用的正确方法是什么?

android - 在 JSON 对象内将请求作为 JSON 参数发送

java - 使用 Glide 库加载音频/音乐文件封面