diff --git a/Gridicons/Gridicons/Gridicons.swift b/Gridicons/Gridicons/Gridicons.swift index ca09ff6..8c26c12 100644 --- a/Gridicons/Gridicons/Gridicons.swift +++ b/Gridicons/Gridicons/Gridicons.swift @@ -156,6 +156,8 @@ public enum GridiconType: Int { public final class Gridicon: NSObject { public static let defaultSize = CGSize(width: 24.0, height: 24.0) + private static let availableSizes = [CGSize(width: 18.0, height: 18.0), defaultSize, CGSize(width: 36.0, height: 36.0), CGSize(width: 48.0, height: 48.0)] + private static let resizingBehavior = GridiconsGenerated.ResizingBehavior.AspectFit private static let cache = NSCache() @@ -170,18 +172,33 @@ public final class Gridicon: NSObject { // These are two separate methods (rather than one method with a default argument) because Obj-C - /// - returns: A template image of the specified Gridicon type, at the specified size. + /// - returns: A template image of the specified Gridicon type, at the default size. If the size + /// -----------specified by the user is not 18x18, 24x24, 36x36 or 48x48. public static func iconOfType(type: GridiconType, withSize size: CGSize) -> UIImage { if let icon = cachedIconOfType(type, withSize: size) { return icon } - let icon = generateIconOfType(type, withSize: size).imageWithRenderingMode(.AlwaysTemplate) - cache.setObject(icon, forKey: "\(type.rawValue)-\(size.width)-\(size.height)") + let icon = generateIconOfType(type, withSize: correctIconSize(size)) + cache.setObject(icon, forKey: "\(type.rawValue)-\(icon.size.width)-\(icon.size.height)") return icon } + private static func correctIconSize(size: CGSize) -> CGSize{ + var correctSize = defaultSize + + if(size == availableSizes[0]) { + correctSize = size + } else if(size == availableSizes[2]) { + correctSize = size + } else if(size == availableSizes[3]) { + correctSize = size + } + + return correctSize + } + private static func cachedIconOfType(type: GridiconType, withSize size: CGSize) -> UIImage? { return cache.objectForKey("\(type.rawValue)-\(size.width)-\(size.height)") as? UIImage } @@ -488,4 +505,4 @@ public final class Gridicon: NSObject { return GridiconsGenerated.imageOfGridiconsaddimage(size: size, resizing: resizingBehavior) } } -} \ No newline at end of file +} diff --git a/Gridicons/GridiconsTests/GridiconsTests.swift b/Gridicons/GridiconsTests/GridiconsTests.swift index 48a4b85..c54be19 100644 --- a/Gridicons/GridiconsTests/GridiconsTests.swift +++ b/Gridicons/GridiconsTests/GridiconsTests.swift @@ -34,7 +34,7 @@ class GridiconsTests: XCTestCase { let size = CGSize(width: 250, height: 250) let icon2 = Gridicon.iconOfType(.UserCircle, withSize: size) - XCTAssertEqual(icon2.size, size) + XCTAssertEqual(icon2.size, Gridicon.defaultSize) } func testSingleIconGenerationPerformance() { diff --git a/GridiconsDemo/GridiconsDemo/ViewController.swift b/GridiconsDemo/GridiconsDemo/ViewController.swift index b6ea5c7..1eaaff8 100644 --- a/GridiconsDemo/GridiconsDemo/ViewController.swift +++ b/GridiconsDemo/GridiconsDemo/ViewController.swift @@ -50,6 +50,12 @@ class ViewController: UIViewController { let value = Int(stepper.value) + if(value == 18) { + stepper.stepValue = 6 + } else { + stepper.stepValue = 12 + } + sizeLabel.text = "\(value)px" iconSize = CGSize(width: CGFloat(value), height: CGFloat(value)) @@ -73,4 +79,4 @@ extension ViewController: UICollectionViewDataSource { return cell } -} \ No newline at end of file +}