2016-04-25 26 views
2

我目前有一個基本的1頁React Native應用程序,用於iOS(iPad 4),它顯示相機饋送併疊加圖像序列。這個圖像序列由149幀組成,並持續循環。如何在React Native中處理圖像序列

我通過每秒24次地替換組件的來源來實現圖像序列循環。

這裏是應用程序類(沒有風格道具)。

class App extends Component { 

    constructor(props) { 
     super(props); 

     this.state = { 
      frame: 1 
     }; 
    } 

    componentDidMount() { 
     setInterval(() => { 
      let frame = this.state.frame; 

      frame++; 

      if (frame > 149) { 
       frame = 1; 
      } 

      this.setState({frame}); 
     }, 1000/24); 
    } 

    render() { 
     return (
      <View style={styles.container}> 
       <Camera ref={(cam) => {this.camera = cam}}> 
        <Text>Frame: {this.state.frame}</Text> 
        <Image source={{uri: `./gifs/${this.state.frame}.gif`}}/> 
       </Camera> 
      </View> 
     ); 
    } 

} 

這給出了這個輸出。

Image of the app.

我得到的問題是,不同的時間長度後的應用程序崩潰。有時它可以在碰撞前運行3秒鐘,有時可以在碰撞前運行2分鐘。

我猜這是一個內存問題,但在Xcode的調試器中,它只使用〜10%的可用內存。有沒有辦法以某種方式只加載我需要的圖像到內存中,並刪除我不或自動管理的圖像?

+0

你好。我想知道如果圖像流自動保存爲GIF,或者你在幕後做了什麼?因爲我正在嘗試訪問原始傳入流數據,並且無法不幸 – allisius

回答

3

嘗試react-native-sprite,它使用本機UIImageView而不是javascript setInterval解決方案來動畫圖像序列。

相關問題