【发布时间】:2018-03-23 08:09:43
【问题描述】:
我使用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} />
【问题讨论】:
-
我不确定这是否是问题所在,但您的路径以
/开头,它将创建类似uri : 'file:///Users/....'的路径尝试从路径中删除/并再次运行. -
那没有成功
-
将 javascript 对象传递给 LottieView 完成了工作!谢谢!
标签: react-native lottie react-native-fs