React-native:如何在 tvOS 中实现图像缓存?

React-native: How to implement caching for image in tvOS?

React-native:如何在 tvOS 中实现图像缓存?

我想为我在互联网上下载的图像实现缓存,如何在 apple tv 上实现?我试过很多图书馆,但任何人都与苹果电视兼容

您必须使用 AsyncStorage 手动进行缓存。您使用 AsyncStorage.setItem 添加必须缓存到存储的对象,并使用 AsyncStorage.getItem 取回对象。这两个函数都将 return promises。

// cache item
try {
  await AsyncStorage.setItem('@MyAppCache:myKey', myItem);
} catch {
  //couldn't cache item, handle it
}
// retrieve item from cache
try {
  const myItem = await AsyncStorage.getItem('@MyAppCache:myKey');
} catch {
  //didn't find item in cache, download from the internet
}

如果这看起来太低级,AsyncStorage 周围有一些非常好的包装器,例如 react-native-storage,它们提供了一些好东西,例如设置缓存项的到期日期、查询批处理和其他东西。

React Native FS is now compatible with tvOS see。所以现在你可以使用任何使用它的库了!

例如 react-native-cacheable-image :

您需要安装 link react-native-fs,只需输入:

npm install react-native-fs --save # install react-native-fs
react-native link react-native-fs #link native module

现在您可以安装 react-native-cacheable-image:

npm i react-native-cacheable-image --save

像这样导入这个库:

import CacheableImage from 'react-native-cacheable-image'

现在您可以使用 JSX:

创建这样的图像
 <CacheableImage
            style={{height:20}}
            source={{uri: "http://www.foobar.com/defaultImage.jpeg"}}
        >

您的设备有一些规格,请参阅 react-native-cacheable-image

的文档

希望我的回答对您有所帮助