iOS原生的CameraRoll加载速度太慢。

时间:2018-11-22 23:13:50

标签: ios react-native camera-roll

使用react-native,我实现了IOS CameraRoll,它首先从“相机胶卷”相册中获取300张图像,并在滚动结束时继续获取300张图像。以下是我的代码。

SalmonCameraRoll.js

import React from 'react'

import {
  View,
  Text,
  TouchableHighlight,
  Modal,
  StyleSheet,
  Button,
  CameraRoll,
  Image,
  Dimensions,
  ScrollView,
  FlatList,
} from 'react-native'

import Share from 'react-native-share';
import RNFetchBlob from 'react-native-fetch-blob';

let styles
const { width, height } = Dimensions.get('window')
const fetchAmount = 300;


class SalmonCameraRoll extends React.Component {
  static navigationOptions = {
    title: 'Salmon Camera Roll',
  }

  constructor(props) {
    super(props);
    this.state = {
      photos: [],
      // index: null,
      lastCursor: null,
      noMorePhotos: false,
      loadingMore: false,
      refreshing: false,
    };
    this.tryGetPhotos = this.tryGetPhotos.bind(this);
    this.getPhotos = this.getPhotos.bind(this);
    this.appendPhotos = this.appendPhotos.bind(this);
    this.renderImage = this.renderImage.bind(this);
    this.onEndReached = this.onEndReached.bind(this);
    this.getPhotos({first: fetchAmount, assetType: 'Photos'});
  }

  componentDidMount() {
    this.subs = [
      this.props.navigation.addListener('didFocus', () => {
        this.getPhotos({first: fetchAmount, assetType: 'Photos'});
      }),
    ];
  }

  componentWillUnmount() {
    this.subs.forEach(sub => sub.remove());
  }

  tryGetPhotos = (fetchParams) => {
    if (!this.state.loadingMore) {
      this.setState({ loadingMore: true }, () => { this.getPhotos(fetchParams)})
    }
  }

  getPhotos = (fetchParams) => {
    if (this.state.lastCursor) {
      fetchParams.after = this.state.lastCursor;
    }

    CameraRoll.getPhotos(fetchParams).then(
      r => this.appendPhotos(r)
    )
  }

  appendPhotos = (data) => {
    const photos = data.edges;
    const nextState = {
      loadingMore: false,
    };

    if (!data.page_info.has_next_page) {
      nextState.noMorePhotos = true;
    }

    if (photos.length > 0) {
      nextState.lastCursor = data.page_info.end_cursor;
      nextState.photos = this.state.photos.concat(photos);
      this.setState(nextState);
    }
  }

  onEndReached = () => {
    if (!this.state.noMorePhotos) {
      this.tryGetPhotos({first: fetchAmount, assetType: 'Photos'});
    }
  }

  renderImage = (photo, index) => {
    return (
      <TouchableHighlight
        style={{borderTopWidth: 1, borderRightWidth: 1, borderColor: 'white'}}
        key={index}
        underlayColor='transparent'
        onPress={() => {
            this.props.navigation.navigate('Camera', { backgroundImageUri: photo.node.image.uri })
          }
        } 
      >
        <Image
          style={{
            width: width/3,
            height: width/3
          }}
          representation={'thumbnail'}
          source={{uri: photo.node.image.uri}}
        />
      </TouchableHighlight>
    )
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.modalContainer}>
            <FlatList
              numColumns={3}
              data={this.state.photos}
              initialNumToRender={fetchAmount}
              onEndReachedThreshold={500}
              onEndReached={this.onEndReached}
              refreshing={this.state.refreshing}
              onRefresh={() => this.tryGetPhotos({first: fetchAmount, assetType: 'Photos'})}
              keyExtractor={(item, index) => index}
              renderItem={({ item, index }) => (
                this.renderImage(item, index)
              )}
            />
        </View>
      </View>
    )
  }
}

styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  modalContainer: {
    // paddingTop: 20,
    flex: 1,
  },
  scrollView: {
    flexWrap: 'wrap',
    flexDirection: 'row'
  },
  shareButton: {
    position: 'absolute',
    width,
    padding: 10,
    bottom: 0,
    left: 0
  }
})

export default SalmonCameraRoll


问题是,在“相机胶卷”相册中有很多图像(约10000张图像)的情况下,每个图像组件的加载速度都很慢,因此在相应滚动时加载速度也太慢。

在其他著名的应用程序(如Facebook或Instagram)中,它可以立即快速加载所有图像,而无需在滚动结束时获取。

如何使我的图像组件快速加载?还是最好的(如果可能的话),如何使我的CameraRoll快速一次加载所有图像,而无需在滚动结束时获取数据?

谢谢。

0 个答案:

没有答案