android - ReactNative怎样实现图片缓存?或者有无现成的组件呢?
巴扎黑
巴扎黑 2017-04-17 15:29:16
[Android讨论组]

使用react-native开发安卓程序时,需要从网络加载图片并显示在列表,测试发现不断刷新后,内存很快撑爆了,应用卡的不行。原因是因为每次刷新都会加载图片到内存,ReactNative中有没有图片缓存的组件呢?

'use strict';
var React = require('react-native');
var {

StyleSheet,
Text,
View,
TouchableHighlight,
Platform,
Alert,
Image
} = React;

var GiftedListView = require('react-native-gifted-listview');
var GiftedSpinner = require('react-native-gifted-spinner');

var DATA = require('../mock/SubjectMockData');
/**

  • 专题页
    */

var Subject = React.createClass({

/**
 * Will be called when refreshing
 * Should be replaced by your own logic
 * @param {number} page Requested page to fetch
 * @param {function} callback Should pass the rows
 * @param {object} options Inform if first load
 */
_onFetch(page, callback, options) {
    setTimeout(() => {
        var rows = JSON.parse(DATA);
        callback(rows, {
            allLoaded: true // the end of the list is reached
        });
    }, 2000); // simulating network fetching
},
/**
 * When a row is touched
 * @param {object} rowData Row data
 */
_onPress(rowData) {
    console.log(rowData + ' pressed');
},

/**
 * Render a row
 * @param {object} rowData Row data
 */
_renderRowView(rowData) {
    var imgUrl = "http:" + rowData.img;
    return (
         this._onPress(rowData)}>
            
        
    );
},

/**
 * Render a row
 * @param {object} rowData Row data
 */
_renderSectionHeaderView(sectionData, sectionID) {
    return null;
},

/**
 * Render the refreshable view when waiting for refresh
 * On Android, the view should be touchable to trigger the refreshCallback
 * @param {function} refreshCallback The function to call to refresh the listview
 */
_renderRefreshableWaitingView(refreshCallback) {
    if (Platform.OS !== 'android') {
        return (
            
                
            
        );
    } else {
        return (
            
                
            
        );
    }
},

/**
 * Render the refreshable view when the pull to refresh has been activated
 * @platform ios
 */
_renderRefreshableWillRefreshView() {
    return (
        
            
        
    );
},

/**
 * Render the refreshable view when fetching
 */
_renderRefreshableFetchingView() {
    return (
        
            
        
    );
},

/**
 * Render the pagination view when waiting for touch
 * @param {function} paginateCallback The function to call to load more rows
 */
_renderPaginationWaitingView(paginateCallback) {
    return (
        
            
                Load more
            
        
    );
},

/**
 * Render the pagination view when fetching
 */
_renderPaginationFetchigView() {
    return (
        
            
        
    );
},

/**
 * Render the pagination view when end of list is reached
 */
_renderPaginationAllLoadedView() {
    return null;
},

/**
 * Render a view when there is no row to display at the first fetch
 * @param {function} refreshCallback The function to call to refresh the listview
 */
_renderEmptyView(refreshCallback) {
    return (
        
            
                Sorry, there is no content to display...
            

            
                
            
        
    );
},

/**
 * Render a separator between rows
 */
_renderSeparatorView() {
    return null;
},

render() {
    return (
        
            
        
    );
}

});

var customStyles = {

separator: {
    height: 1,
    backgroundColor: '#CCC'
},
refreshableView: {
    height: 50,
    backgroundColor: '#fff',
    justifyContent: 'center',
    alignItems: 'center',
},
actionsLabel: {
    fontSize: 20,
    color: '#007aff',
},
paginationView: {
    height: 44,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFF',
},
defaultView: {
    justifyContent: 'center',
    alignItems: 'center',
    padding: 20,
},
defaultViewTitle: {
    fontSize: 16,
    fontWeight: 'bold',
    marginBottom: 15,
},
row: {
    height: 200,
    backgroundColor: 'skyblue',
    borderRadius: 5,
    marginTop: 10,
    marginLeft: 10,
    marginRight: 10,
},
header: {
    backgroundColor: '#50a4ff',
    padding: 10,
},
headerTitle: {
    color: '#fff',
},

};

var screenStyles = {

container: {
    flex: 1,
    backgroundColor: '#FFF',
},
navBar: {
    height: 64,
    backgroundColor: '#007aff',

    justifyContent: 'center',
    alignItems: 'center',
},
navBarTitle: {
    color: '#fff',
    fontSize: 16,
    marginTop: 12,
}

};

巴扎黑
巴扎黑

全部回复(1)
大家讲道理

先把你的代码贴出来,或者贴部分出来,
让大家品评品评你写的是不是RN代码。
别又象上次一样来个WebView大包大揽。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号