You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. //
  2. // FilepathImageProvider.swift
  3. // lottie-swift
  4. //
  5. // Created by Brandon Withrow on 2/1/19.
  6. //
  7. import Foundation
  8. #if os(iOS) || os(tvOS) || os(watchOS) || targetEnvironment(macCatalyst)
  9. import UIKit
  10. /// Provides an image for a lottie animation from a provided Bundle.
  11. public class FilepathImageProvider: AnimationImageProvider {
  12. // MARK: Lifecycle
  13. /// Initializes an image provider with a specific filepath.
  14. ///
  15. /// - Parameter filepath: The absolute filepath containing the images.
  16. ///
  17. public init(filepath: String) {
  18. self.filepath = URL(fileURLWithPath: filepath)
  19. }
  20. public init(filepath: URL) {
  21. self.filepath = filepath
  22. }
  23. // MARK: Public
  24. public func imageForAsset(asset: ImageAsset) -> CGImage? {
  25. if
  26. asset.name.hasPrefix("data:"),
  27. let url = URL(string: asset.name),
  28. let data = try? Data(contentsOf: url),
  29. let image = UIImage(data: data)
  30. {
  31. return image.cgImage
  32. }
  33. let directPath = filepath.appendingPathComponent(asset.name).path
  34. if FileManager.default.fileExists(atPath: directPath) {
  35. return UIImage(contentsOfFile: directPath)?.cgImage
  36. }
  37. let pathWithDirectory = filepath.appendingPathComponent(asset.directory).appendingPathComponent(asset.name).path
  38. if FileManager.default.fileExists(atPath: pathWithDirectory) {
  39. return UIImage(contentsOfFile: pathWithDirectory)?.cgImage
  40. }
  41. LottieLogger.shared.warn("Could not find image \"\(asset.name)\" in bundle")
  42. return nil
  43. }
  44. // MARK: Internal
  45. let filepath: URL
  46. }
  47. #endif