Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit fbc3542

Browse files
committed
Introduce ImageResource
1 parent 5cad4e0 commit fbc3542

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Library/Core/ImageResource.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// ImageResource.swift
3+
// R.swift.Library
4+
//
5+
// Created by Mathijs Kadijk on 11-01-16.
6+
// Copyright © 2016 Mathijs Kadijk. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public struct ImageResource {
12+
13+
/// Bundle this image is in or nil for main bundle
14+
public let bundle: NSBundle?
15+
16+
/// Name of the image
17+
public let name: String
18+
19+
public init(bundle: NSBundle?, name: String) {
20+
self.bundle = bundle
21+
self.name = name
22+
}
23+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// UIImage+ImageResource.swift
3+
// R.swift.Library
4+
//
5+
// Created by Mathijs Kadijk on 11-01-16.
6+
// Copyright © 2016 Mathijs Kadijk. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
public extension UIImage {
12+
/**
13+
Returns the image from this resource (R.image.*) that is compatible with the trait collection.
14+
15+
- parameter resource: The resource you want the image of (R.image.*)
16+
- parameter traitCollection: Traits that describe the desired image to retrieve, pass nil to use traits that describe the main screen.
17+
18+
- returns: An image that exactly or best matches the desired traits with the given resource (R.image.*), or nil if no suitable image was found.
19+
*/
20+
public convenience init?(resource: ImageResource, compatibleWithTraitCollection traitCollection: UITraitCollection? = nil) {
21+
self.init(named: resource.name, inBundle: resource.bundle, compatibleWithTraitCollection: traitCollection)
22+
}
23+
}

R.swift.Library.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
D543F9C81C14995800D16A0C /* UIViewController+NibResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D543F9C71C14995800D16A0C /* UIViewController+NibResource.swift */; };
1818
D543F9CA1C14998800D16A0C /* UIViewController+StoryboardSegueIdentifierProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = D543F9C91C14998800D16A0C /* UIViewController+StoryboardSegueIdentifierProtocol.swift */; };
1919
D543F9CF1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D543F9CE1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift */; };
20+
D553F5851C44157000885232 /* ImageResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D553F5841C44157000885232 /* ImageResource.swift */; };
21+
D553F5871C44170E00885232 /* UIImage+ImageResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D553F5861C44170E00885232 /* UIImage+ImageResource.swift */; };
2022
D5588CAB1C3F9DBE00912F97 /* UINib+NibResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5588CAA1C3F9DBE00912F97 /* UINib+NibResource.swift */; };
2123
D56DC7731C42B65C00623437 /* NSBundle+FileResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D56DC7721C42B65C00623437 /* NSBundle+FileResource.swift */; };
2224
D57E1EB31C3D762300DDA68F /* FontResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57E1EB21C3D762300DDA68F /* FontResource.swift */; };
@@ -52,6 +54,8 @@
5254
D543F9C71C14995800D16A0C /* UIViewController+NibResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+NibResource.swift"; sourceTree = "<group>"; };
5355
D543F9C91C14998800D16A0C /* UIViewController+StoryboardSegueIdentifierProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+StoryboardSegueIdentifierProtocol.swift"; sourceTree = "<group>"; };
5456
D543F9CE1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TypedStoryboardSegueInfo+UIStoryboardSegue.swift"; sourceTree = "<group>"; };
57+
D553F5841C44157000885232 /* ImageResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageResource.swift; sourceTree = "<group>"; };
58+
D553F5861C44170E00885232 /* UIImage+ImageResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+ImageResource.swift"; sourceTree = "<group>"; };
5559
D5588CAA1C3F9DBE00912F97 /* UINib+NibResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UINib+NibResource.swift"; sourceTree = "<group>"; };
5660
D56DC7721C42B65C00623437 /* NSBundle+FileResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBundle+FileResource.swift"; sourceTree = "<group>"; };
5761
D57E1EB21C3D762300DDA68F /* FontResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FontResource.swift; sourceTree = "<group>"; };
@@ -101,6 +105,7 @@
101105
D543F9C31C1498FB00D16A0C /* UITableView+ReuseIdentifierProtocol.swift */,
102106
D543F9C71C14995800D16A0C /* UIViewController+NibResource.swift */,
103107
D543F9C91C14998800D16A0C /* UIViewController+StoryboardSegueIdentifierProtocol.swift */,
108+
D553F5861C44170E00885232 /* UIImage+ImageResource.swift */,
104109
);
105110
path = UIKit;
106111
sourceTree = "<group>";
@@ -116,6 +121,7 @@
116121
D57E1EB61C3E482A00DDA68F /* StoryboardResource.swift */,
117122
D543F9BE1C14983100D16A0C /* StoryboardSegueIdentifierProtocol.swift */,
118123
D53F19231C229D7200AE2FAD /* Validatable.swift */,
124+
D553F5841C44157000885232 /* ImageResource.swift */,
119125
);
120126
path = Core;
121127
sourceTree = "<group>";
@@ -284,8 +290,10 @@
284290
D57E1EB91C3E4C1A00DDA68F /* StoryboardResourceWithInitialController+UIKit.swift in Sources */,
285291
D543F9BD1C14980600D16A0C /* ReuseIdentifierProtocol.swift in Sources */,
286292
D543F9C11C14984300D16A0C /* NibResource.swift in Sources */,
293+
D553F5851C44157000885232 /* ImageResource.swift in Sources */,
287294
D57E1EB51C3D774000DDA68F /* UIFont+FontResource.swift in Sources */,
288295
D5588CAB1C3F9DBE00912F97 /* UINib+NibResource.swift in Sources */,
296+
D553F5871C44170E00885232 /* UIImage+ImageResource.swift in Sources */,
289297
D543F9C61C14992000D16A0C /* UICollectionView+ReuseIdentifierProtocol.swift in Sources */,
290298
D543F9BF1C14983100D16A0C /* StoryboardSegueIdentifierProtocol.swift in Sources */,
291299
D543F9C81C14995800D16A0C /* UIViewController+NibResource.swift in Sources */,

0 commit comments

Comments
 (0)