Skip to content

Commit 83970ee

Browse files
committed
CM-965: 'loadImage()'. and image added in 'ImageAsset' extension
1 parent 075d91d commit 83970ee

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Sources/YCoreUI/Protocols/ImageAsset.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010
import UIKit
1111

12-
public protocol ImageAsset {
12+
public protocol ImageAsset: RawRepresentable where RawValue == String {
1313
/// The bundle containing the image assets (default is `.main`)
1414
static var bundle: Bundle { get }
1515

@@ -36,4 +36,24 @@ extension ImageAsset {
3636

3737
/// Optional namespace for the image assets (default is `nil`)
3838
public static var namespace: String? { nil }
39+
40+
/// Loads the named image
41+
///
42+
/// Default implementation uses `UIImage(named:in:compatibleWith:)` passing in the associated `namespace`
43+
/// (prepended to `rawValue`) and `bundle`.
44+
/// - Returns: The named image or else `nil` if the named asset cannot be loaded
45+
public func loadImage() -> UIImage? {
46+
let name: String
47+
if let validNamespace = Self.namespace {
48+
name = "\(validNamespace)/\(rawValue)"
49+
} else {
50+
name = rawValue
51+
}
52+
return UIImage(named: name, in: Self.bundle, compatibleWith: nil)
53+
}
54+
55+
/// An image asset for this name value.
56+
///
57+
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`.
58+
public var image: UIImage { loadImage() ?? Self.fallbackImage }
3959
}

0 commit comments

Comments
 (0)