React Native图片缓存组件react-native-rn-cacheimage

额外推荐,RN常用第三方库https://zhuanlan.zhihu.com/p/69154149

共两种方案:

一、react-native-rn-cacheimage

今天介绍一个React Native的图片缓存组件 react-native-rn-cacheimage ,纯JS实现,所以兼容性很好。

大家都知道,其实React Native 的 Image 组件在 iOS 端实现了缓存,而android 端仍未实现,而且,就算实现了 iOS端 ,可能有些需求仍然比较难实现,比如一般APP都有一个 清除缓存 的功能,如果我们使用默认的 Image 的缓存实现,我们能难定位图片到底缓存在本地文件系统的哪个目录。 react-native-rn-cacheimage 的实现方式是,把所有的缓存图片都放在一个指定的文件夹下,并提供了一个方法 CacheHelper.clearCache() 方法能够轻松清除缓存。

下面我们就来介绍下它的安装及使用

安装

rn-fetch-blob

react-native-rn-cacheimage 使用到了 rn-fetch-blob 这个package,由于 rn-fetch-blob 的实现涉及到了 native 代码,所以安装会比较复杂,强烈建议按照 官方安装手册 来安装。当然,一般情况下使用以下两个命令来安装就可以:

 

$ npm install rn-fetch-blob --save
$ react-native link rn-fetch-blob

如果有问题,建议按照官方安装手册 的手动link的方式来安装。

react-native-rn-cacheimage

由于这个package本身是纯 js 来实现的,没有涉及iOSandroid 的本地代码,所以安装很简单:

 

$ npm install react-native-cacheimage --save

使用

Register和unregister

  • 在使用 CacheImageAnimatedCacheImage 之前,需要初始化相关信息,建议在APP嘴顶层的 componentcomponentDidMount() 中初始化
  • 在APP的顶层 componentcomponentWillUnmount()中,执行清除任务

具体操作,见如下代码 :

 

import React from 'react'
import {AppRegistry} from 'react-native'
import {Provider} from 'react-redux'
import ReduxStore from './src/configuration/reduxStore'
import App from './src/App'
import {CacheHelper} from "react-native-rn-cacheimage";

const store = ReduxStore

class MyApp extends React.Component {
    componentDidMount() {
        CacheHelper.register({overwrite:false}).catch(e => console.log(e))
    }
    
    componentWillUnmount() {
        CacheHelper.unregister().catch(e=>console.log(e))
    }

    render() {
        return (
            <Provider store={store}>
                <App/>
            </Provider>
        )
    }
}

AppRegistry.registerComponent("YourAppName", () => MyApp);

使用CacheImage和AnimatedCacheImage组件

CacheImage组件

CacheImage 组件可以代替原有的 ImageImageBackground 组件使用,并且 propsImageImageBackground 参数保持一致.

 

import {CacheImage} from 'react-native-rn-cacheimage'
export default class Example extends React.Component {
...
    render() {
        
        return 
        (
        <View>
        <CacheImage
            source={{uri:'https://xxx.xxx'}}
            defaultSource={require('/xxx/xxx.png')}
            style={styles.image}
        />
        <CacheImage
            source={{uri:'https://xxx.xxx'}}
            defaultSource={require('/xxx/xxx.png')}
            style={styles.image}
        >
            <Text>Hello World!</Text>
        </CacheImage>
        </View>
        )
        
    }
...
}

AnimatedCacheImage组件

AnimatedCacheImage 可以 代替 Animated.Image组件,并且所有参数与 Animated.Image 保持一致

 

import {AnimatedCacheImage} from 'react-native-rn-cacheimage'

export default class Example extends React.Component {
...
    render() {
        
        return 
        (
        <View>
        <AnimatedCacheImage
            source={{uri:'https://xxx.xxx.png'}}
            defaultSource={require('/xxx/xxx.png')}
            style={styles.image}
        />
        </View>
        )
        
    }
...
}

CacheHelper的API

CacheHelper 是一个辅助类,里面包含一些工具方法,大家可以根据自己的需求,选择调用。这里就不全部列出来,大家可以直接到 Github查看

getCacheSize():Promise<Number>

获取所有的缓存图片所占用内存大小,返回的数字结果单位是byte

getCacheSizeFormat():Promise<String>

这个与 getCacheSize() 很相似,只不过它的返回结果是已经格式化好的,比如:10.2MB98KB

clearCache():Promise<Void>

清空缓存。一般我们APP都会有一个清空缓存的功能,我们可以调用这个方法清空我们使用这个 package 所产生的缓存文件。

最后

这个图片缓存组件 react-native-rn-cacheimage 就介绍到这里,如果大家想要详细了解,可以直接去 Github 上查看具体说明及源码,如果发现bug可以可以直接提 issue ,或者直接在文章下面提问。

推荐阅读



作者:Quenice
链接:https://www.jianshu.com/p/19ef9f32c332
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

二、react-native-img-cache

1. react-native-fetch-blob 将图片存在本地的一个东西

 

2. react-native-img-cache 自动缓存的一个东西

上面装好后 就可以使用啦

import {CachedImage} from "react-native-img-cache";

<CachedImage source={{ uri: "https://i.ytimg.com/vi/yaqe1qesQ8c/maxresdefault.jpg" }} />

 

如果想让图片有种加载效果

import Image from 'react-native-image-progress';

import ProgressBar from 'react-native-progress/Bar';

 

最后使用

import {CustomCachedImage} from "react-native-img-cache";

import Image from 'react-native-image-progress';

import ProgressBar from 'react-native-progress/Bar';
<CustomCachedImage

  component={Image}

  source={{ uri: 'http://loremflickr.com/640/480/dog' }} 

  indicator={ProgressBar} 

  style={{

    width: 320, 

    height: 240, 

  }}/>


链接:https://www.cnblogs.com/allenxieyusheng/p/9122747.html
来源:cnblogs
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

传送门:https://github.com/wcandillon/react-native-img-cache

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React Native提供了一些用于缩放图片组件,其中之一是`react-native-safe-area-context`库中的`SafeAreaImage`组件。这个组件可以帮助你在设备边缘安全区域内展示图片,并允许你进行缩放操作。 要使用`SafeAreaImage`组件进行图片缩放,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了`react-native-safe-area-context`库。你可以使用npm或yarn进行安装: ```bash npm install react-native-safe-area-context ``` 或 ```bash yarn add react-native-safe-area-context ``` 2. 在你的React Native应用程序中引入`SafeAreaImage`组件: ```javascript import SafeAreaImage from 'react-native-safe-area-context'; ``` 3. 在需要展示缩放图片组件中使用`SafeAreaImage`组件,并设置相关属性。例如,你可以使用`style`属性设置图片的尺寸和位置,使用`resizeMode`属性设置图片的缩放模式,以及使用`source`属性设置图片的来源。 ```javascript <SafeAreaImage style={{ width: 200, height: 200 }} resizeMode="center" source={require('./path/to/your/image.png')} /> ``` 在上面的示例中,我们设置了图片的宽度和高度为200像素,并使用`resizeMode="center"`将图片居中显示。我们还通过`source`属性指定了图片的来源路径。 需要注意的是,上述示例中的路径是一个相对路径,你需要根据实际情况修改为你的图片文件的实际路径。另外,你还可以根据需要设置其他样式和属性来定制缩放图片的外观和行为。 除了`react-native-safe-area-context`库提供的`SafeAreaImage`组件React Native还提供了其他一些用于缩放图片组件和第三方库,如`react-native-responsive-image`、`react-native-safe-area-image`等。你可以根据需要选择适合你的库或组件来进行图片缩放操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值