【问题标题】:What's the correct way to read Lottie animation from document directory从文档目录中读取 Lottie 动画的正确方法是什么
【发布时间】: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)

标签: ios swift lottie


【解决方案1】:

您使用 filePath 的方式存在问题。由于 pathWithFilename 是 URL,所以不要像 "\(pathWithFilename)" 那样使用它,而应该像 pathWithFilename.path 那样使用它。

以下内容对你有用,

let checkMarkAnimation =  AnimationView(filePath: pathWithFilename.path)
animationView.contentMode = .scaleAspectFit
self.animationView.addSubview(checkMarkAnimation)
checkMarkAnimation.frame = self.animationView.bounds
checkMarkAnimation.loopMode = .loop
checkMarkAnimation.play()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2015-02-21
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多