BundleImageProvider.swift 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
//
//  LottieBundleImageProvider.swift
//  lottie-swift
//
//  Created by Brandon Withrow on 1/25/19.
//

#if canImport(UIKit)
import UIKit

/// An `AnimationImageProvider` that provides images by name from a specific bundle.
/// The BundleImageProvider is initialized with a bundle and an optional searchPath.
public class BundleImageProvider: AnimationImageProvider {

  // MARK: Lifecycle

  /// Initializes an image provider with a bundle and an optional subpath.
  ///
  /// Provides images for an animation from a bundle. Additionally the provider can
  /// search a specific subpath for the images.
  ///
  /// - Parameter bundle: The bundle containing images for the provider.
  /// - Parameter searchPath: The subpath is a path within the bundle to search for image assets.
  /// - Parameter contentsGravity: The contents gravity to use when rendering the image.
  ///
  public init(bundle: Bundle, searchPath: String?, contentsGravity: CALayerContentsGravity = .resize) {
    self.bundle = bundle
    self.searchPath = searchPath
    self.contentsGravity = contentsGravity
  }

  // MARK: Public

  public func imageForAsset(asset: ImageAsset) -> CGImage? {
    if let base64Image = asset.base64Image {
      return base64Image
    }

    let imagePath: String?
    /// Try to find the image in the bundle.
    if let searchPath {
      /// Search in the provided search path for the image
      var directoryPath = URL(fileURLWithPath: searchPath)
      directoryPath.appendPathComponent(asset.directory)

      if let path = bundle.path(forResource: asset.name, ofType: nil, inDirectory: directoryPath.path) {
        /// First search for the image in the asset provided sub directory.
        imagePath = path
      } else if let path = bundle.path(forResource: asset.name, ofType: nil, inDirectory: searchPath) {
        /// Try finding the image in the search path.
        imagePath = path
      } else {
        imagePath = bundle.path(forResource: asset.name, ofType: nil)
      }
    } else {
      if let path = bundle.path(forResource: asset.name, ofType: nil, inDirectory: asset.directory) {
        /// First search for the image in the asset provided sub directory.
        imagePath = path
      } else {
        /// First search for the image in bundle.
        imagePath = bundle.path(forResource: asset.name, ofType: nil)
      }
    }

    if imagePath == nil {
      guard let image = UIImage(named: asset.name, in: bundle, compatibleWith: nil) else {
        LottieLogger.shared.warn("Could not find image \"\(asset.name)\" in bundle")
        return nil
      }
      return image.cgImage
    }

    guard let foundPath = imagePath, let image = UIImage(contentsOfFile: foundPath) else {
      /// No image found.
      LottieLogger.shared.warn("Could not find image \"\(asset.name)\" in bundle")
      return nil
    }
    return image.cgImage
  }

  public func contentsGravity(for _: ImageAsset) -> CALayerContentsGravity {
    contentsGravity
  }

  // MARK: Internal

  let bundle: Bundle
  let searchPath: String?
  let contentsGravity: CALayerContentsGravity
}

extension BundleImageProvider: Equatable {
  public static func ==(_ lhs: BundleImageProvider, _ rhs: BundleImageProvider) -> Bool {
    lhs.bundle == rhs.bundle
      && lhs.searchPath == rhs.searchPath
  }
}
#endif