我用来react-native-fs
从远程服务器下载 Lottie 文件 (json)。将其保存到文件系统后,我得到一个类似/Users/marlon/Library/Developer/CoreSimulator/Devices/8E0A092D0E86/data/Containers/Data/Application/AADD60D8DFAD/Documents/animation.json
.
现在有什么方法可以将该文件路径用作源LottieView
?我尝试了各种方法,但都没有成功:
var path = '/Users/marlon/Library/Developer/CoreSimulator/Devices/8E0A092D0E86/data/Containers/Data/Application/AADD60D8DFAD/Documents/animation.json'
<LottieView source={{ uri: path }} />
<LottieView source={{ uri: 'file:/' + path }} />
<LottieView source={{ uri: 'file://' + path }} />
<LottieView source={{ uri: 'file://' + path, isStatic: true }} />
回答
只需将动画文件作为 javascript 对象传递给LottieView
. 所以我现在所做的就是用 . 打开文件react-native-fs
并用JSON.parse
. 最终结果如下所示:
RNFS.readFile(animations.congratsAnimation).then(res => {
this.setState({ animation: JSON.parse(res) });
}
...
<LottieView source={this.state.animation} />