Skip to content

Instantly share code, notes, and snippets.

@Polenoso
Last active May 10, 2021 14:24
Show Gist options
  • Save Polenoso/f58a1bc33dbf792cfe0f51d5500f971e to your computer and use it in GitHub Desktop.
Save Polenoso/f58a1bc33dbf792cfe0f51d5500f971e to your computer and use it in GitHub Desktop.
Lottie Animation From URL
/**
Loads a Lottie animation asynchronously from the URL
- Parameter url: The url to load the animation from.
- Parameter imageProvider: An image provider for the animation's image data.
If none is supplied Lottie will search in the main bundle for images.
- Parameter closure: A closure to be called when the animation has loaded.
*/
convenience init(url: URL,
imageProvider: AnimationImageProvider? = nil,
closure: @escaping AnimationView.DownloadClosure,
animationCache: AnimationCacheProvider? = LRUAnimationCache.sharedCache) {
if let animationCache = animationCache, let animation = animationCache.animation(forKey: url.absoluteString) {
self.init(animation: animation, imageProvider: imageProvider)
closure(nil)
} else {
self.init(animation: nil, imageProvider: imageProvider)
Animation.loadedFrom(url: url, closure: { (animation) in
if let animation = animation {
self.animation = animation
closure(nil)
} else {
closure(LottieDownloadError.downloadFailed)
}
}, animationCache: animationCache)
}
}