File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed
Sources/YCoreUI/Protocols Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change 99import Foundation
1010import 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}
You can’t perform that action at this time.
0 commit comments