【发布时间】:2020-11-04 12:02:13
【问题描述】:
我正在尝试将 json 存储在文档目录中,然后读取以显示 Lottie 动画。
这就是我在文档中写 json 的方式
if let documentDirectory = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask).first {
let pathWithFilename = documentDirectory.appendingPathComponent("myJsonString.json")
do {
try jsonString.write(to: pathWithFilename, atomically: true, encoding: .utf8)
} catch {
}
}
这就是我的阅读方式
let checkMarkAnimation = AnimationView(filePath: "\(pathWithFilename)")
animationView.contentMode = .scaleAspectFit
self.animationView.addSubview(checkMarkAnimation)
checkMarkAnimation.frame = self.animationView.bounds
checkMarkAnimation.loopMode = .loop
checkMarkAnimation.play()
我完全知道这是工作的 json,当我打印 Data(contentsOf: pathWithFilename) 时,它也会打印实际的 json,但是当我用 AnimationView(filePath:) 在动画中分配它时它不起作用。
【问题讨论】:
-
尝试
let checkMarkAnimation = Animation.filepath(yourfilepath),然后分配给animationView.animation = checkMarkAnimation -
@KishanBhatiya 不起作用
-
你得到文件路径了吗?
-
可以,也可以取其内容
-
我已经按照 Kishan Bhatiya 的说法进行了尝试,并且正在为我工作。我认为您使用它的方式存在一些问题。尝试使用类似的路径 - AnimationView(filePath: pathWithFilename.path)