Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #3 Fix #8

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions Gridicons/Gridicons/Gridicons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
Expand Down Expand Up @@ -488,4 +505,4 @@ public final class Gridicon: NSObject {
return GridiconsGenerated.imageOfGridiconsaddimage(size: size, resizing: resizingBehavior)
}
}
}
}
2 changes: 1 addition & 1 deletion Gridicons/GridiconsTests/GridiconsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion GridiconsDemo/GridiconsDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -73,4 +79,4 @@ extension ViewController: UICollectionViewDataSource {

return cell
}
}
}