12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- //
- // LLAnimatableImageView.swift
- import UIKit
- class LLAnimatableImageView: UIImageView {
-
- var cache = LLCache.shared
-
- private var url: URL?
-
- private var worker: DispatchWorkItem?
-
- private var activityIndicator: UIActivityIndicatorView = {
- let ai = UIActivityIndicatorView()
- ai.translatesAutoresizingMaskIntoConstraints = false
- ai.hidesWhenStopped = true
- return ai
- }()
-
- init() {
- super.init(frame: .zero)
-
- self.addSubview(activityIndicator)
-
- NSLayoutConstraint.activate([
- activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor),
- activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor)
- ])
-
-
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
-
- func loadGifFromUrl(urlString: String) {
-
- guard let url = URL(string: urlString) else { return }
- self.url = url
-
- self.cancelAndClear()
-
- self.cache.retreiveDataFromUrl(url: url) {[weak self] gifImage in
-
- gifImage?.prepareForDisplay(completionHandler: { preparedImage in
- DispatchQueue.main.async {
- self?.activityIndicator.stopAnimating()
- self?.image = preparedImage
- }
- })
- }
- }
- /// Sets the current image to nil
- func cancelAndClear() {
-
- self.activityIndicator.startAnimating()
- self.image = nil
- cache.cancelTaskFor(url: url!)
- }
-
- }
|